January 26th, 2010, 04:51 PM
Is it possible to convert the input you get from using the getline(inputfile, string) to an int?
Say i have a file "abcd.txt", with the following content:
How can i convert the 6 and 3, which are strings because of the getline() function, to an integer?
Thank you very much,
January 26th, 2010, 04:55 PM
There are many ways to convert a string to an int. Some may be better than others for various situations:
to name just a few.
January 26th, 2010, 05:03 PM
Thanks for your quick reply, i get an error whilst compiling though.
Heres my code:
using namespace std;
one = atoi(line);
two = atoi(line);
The error i get is the following: error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'
January 26th, 2010, 05:19 PM
Yes, atoi() is a C function, so it doesn't know about std::string. The error is telling you exactly what's wrong. You'll need to use std::string's compatibility method, c_str().
January 26th, 2010, 05:27 PM
Are there other chars on each line besides what you have shown ?
if not, whu not read directly into an int variable ?
January 26th, 2010, 05:29 PM
Thank you, Lindley. That helped!
@ Philip, is there a way i can read it directly into int?
January 26th, 2010, 06:10 PM
int one , two;
diagram >> one >> two;
Tags for this Thread
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.