943,576 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5363
  • C++ RSS
Aug 4th, 2008
0

HELP!! how do you use atoi?

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Aug 4th, 2008
0

Re: HELP!! how do you use atoi?

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 4th, 2008
0

Re: HELP!! how do you use atoi?

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?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008
Aug 4th, 2008
1

Re: HELP!! how do you use atoi?

the string::c_str() method converts the contents of an std::string to const char*. For example:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 77
Solved Threads: 40
Posting Pro in Training
CoolGamer48 is offline Offline
401 posts
since Jan 2008
Aug 4th, 2008
0

Re: HELP!! how do you use atoi?

Thank you.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
CPPRULZ is offline Offline
91 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Visual C++ New form
Next Thread in C++ Forum Timeline: program to generate random numbers for a given range





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC