I retrieve a string from a file that contains only numbers. Now I want to convert it into a integer form to perform arithmetic operations on it.
Is there a way to do so?
PS: I have tried using atoi and it doesn't work. It gives the error: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'
@Framework: That worked. Thanks! But what does c_str() do?
@ultifinitus&webJose: could you tell me how to use stringstreams for string to int conversion?
The link posted be WebJose was a bit too advanced for me.
Streams know how to convert many (if not all) native data types to a string. So basically, you use a special stream that bases its storage in a string object (therefore the name "stringstream") to "input" and "output" strings to and from data types. That's all. The rest that you see in the code is to make sure, for example, that all data in the string is used in the conversion (see the function FromString()). The rest is code that makes sure the conversion succeeded and that the correct number of digits are shown.