HELP!! how do you use atoi?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

HELP!! how do you use atoi?

 
0
  #1
Aug 4th, 2008
I'm trying to figure out how to use the atoi function I have wrote and understand this so far:
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
int x;
string str="34";
x=atoi(str);
cout<<x;
system("pause");
return 0;
}
And Then I get error reports about a freakin const char?! HELP!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: HELP!! how do you use atoi?

 
0
  #2
Aug 4th, 2008
Always post the exact errors you get for the best help.

atio() takes input of type const char* .

do this:
x=atoi(str.c_str());

Or, better yet, use stringstreams:
  1. #include <sstream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. stringstream ss;
  7. int x;
  8. string str = "34";
  9. ss << str;
  10. ss >> x;
  11.  
  12. return 0;
  13. }
Last edited by CoolGamer48; Aug 4th, 2008 at 2:58 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: HELP!! how do you use atoi?

 
0
  #3
Aug 4th, 2008
Thank you so much-it works now but I still don't get why you need the .c_str()-does it make it refer to str as a constant string?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 401
Reputation: CoolGamer48 is on a distinguished road 
Solved Threads: 40
CoolGamer48's Avatar
CoolGamer48 CoolGamer48 is offline Offline
Posting Pro in Training

Re: HELP!! how do you use atoi?

 
1
  #4
Aug 4th, 2008
the string::c_str() method converts the contents of an std::string to const char*. For example:
  1. string str1 = "Hello";
  2. const char* str2 = "Hello";
  3. str1.c_str();//would return a value equivalent to str2

further information here

You'd be better off using string streams though. there isn't a need to convert an std::string into an old C-string (const char*). string streams can handle std::strings and C-strings.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 91
Reputation: CPPRULZ is an unknown quantity at this point 
Solved Threads: 0
CPPRULZ CPPRULZ is offline Offline
Junior Poster in Training

Re: HELP!! how do you use atoi?

 
0
  #5
Aug 4th, 2008
Thank you.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC