Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 9th, 2005, 12:32 AM   #1
HackeZ
Programmer
 
Join Date: Dec 2004
Location: Ontario, Canada.
Posts: 38
Rep Power: 0 HackeZ is on a distinguished road
Conversion problem. string type to type int.

Alright Ive Used google for like 5 hours now. I know to convert to char * is c_str() but how would I go about changing this type to a int? Any Help Shown appreciated.
HackeZ is offline   Reply With Quote
Old Dec 9th, 2005, 12:58 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
I assume you want to convert a string like "42" into the value 42.

In vanilla C, this would be done by;
#include <stdlib.h>

int main()
{
    const char v[] = "42";
    int x;
    x = atoi(v);
}
That is deprecated in C++ (i.e. it is allowed, but discouraged and may be removed from a future C++ standard). The C++ equivalent would be;
#include <string>
#include <cstdlib>

int main()
{
    std::string v("42");
    int x;
    x = std::atoi(v.c_str());
}
If you want to do more complex things then you have a few choices, such as;

sscanf() is a C function that reads data from a C-style string

in C++, look up the ostringstream class. An example of it's usage follows;
#include <string>
#include <sstream>
#include <ostream>

int main()
{
    std::string v("42");
    std::ostringstream str(v);
    int x;
    str >> x;   // will get value 42
}
E&OE: I've written the above examples quickly, so may have introduced minor errors. You can also remove the need for "std::" prefixes by judicious use of "using namespace std;", but I prefer code that is guaranteed not to be ambiguous.
grumpy is offline   Reply With Quote
Old Dec 9th, 2005, 9:48 AM   #3
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
These string streams look quite interesting. Turns out you want an istringstream though, since that acts as a 'input' from a string, i.e. allows you to read from it.

This runs:

#include <string>
#include <sstream>
//#include <ostream>
#include <iostream>

int main()
{
    std::string v("42");
    std::istringstream str(v);
    int x;
    str >> x;   // will get value 42
    
    std::cout << x << std::endl;
    std::cin.sync();
    std::cin.get();
}
Klipt is offline   Reply With Quote
Old Dec 9th, 2005, 6:30 PM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Ah yes, indeed. That's what happens when I rattle off an answer quickly.
grumpy is offline   Reply With Quote
Old Dec 14th, 2005, 7:52 PM   #5
HackeZ
Programmer
 
Join Date: Dec 2004
Location: Ontario, Canada.
Posts: 38
Rep Power: 0 HackeZ is on a distinguished road
Thanks for the support guys. Appreciated.
HackeZ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC