943,942 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2127
  • C++ RSS
Apr 3rd, 2005
0

How do i fix this?

Expand Post »
How do you convert a string to an int? In my book under string functions, it shows this short example:

C++ Syntax (Toggle Plain Text)
  1. ia = atoi ("-123.45xyz");

which says that ia = 123.

So, I tried applying this to my code.
C++ Syntax (Toggle Plain Text)
  1. index = atoi (a.GetCode());
  2. cout<<description[index];
  3.  
  4. //a.GetCode() returns a number string 0 - 9
  5. //description[] is an array that holds the job
  6. descriptions to the corresponding code

I included the <cstdlib> like the book says but i get this error message when I compiled it with the "CC" compiler:

C++ Syntax (Toggle Plain Text)
  1. Error: Could not find a match for std::atoi(std::basic_string<char, std::char_traits<char>, std::allocator<char>>)

what does this mean? how do i fix it? is there any other way to convert to an int?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gracieLou is offline Offline
1 posts
since Apr 2005
Apr 3rd, 2005
0

Re: How do i fix this?

You may want to look up strtod or strto[u]l. If you are trying to use these C-style functions with a std::string, look at the c_str() member function too.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 4th, 2005
0

Re: How do i fix this?

I think that your problem is you are trying to pass a string object to the atoi function.

the way that your book uses it is atoi(char *) using a C-style string, not a string object. I'm not sure if it does have an overloaded version using the string object. What you could do is just have your function return yourString.c_str() and it should work.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005

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: Hecadecimal
Next Thread in C++ Forum Timeline: please i need your help.





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


Follow us on Twitter


© 2011 DaniWeb® LLC