954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do i fix this?

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

ia = atoi ("-123.45xyz");

which says that ia = 123.

So, I tried applying this to my code.

index = atoi (a.GetCode());  
            cout<<description[index];
         
            //a.GetCode() returns a number string 0 - 9
           //description[] is an array that holds the job
              descriptions to the corresponding code


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

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?

gracieLou
Newbie Poster
1 post since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

marinme
Junior Poster in Training
63 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You