i see that you are trying to save the line as a string. i don't think that's necessary. i'm not entirely an expert, and i've never tried this sort of thing before, but i think you can just directly store the number as an "int" variable with:
1 2 3
|
int input_from_file; //obviously give it a better name
ifstream myfile("LOCATION"); //location is the location of your file
myfile >> input_from_file
|
the myfile >> input_from_file statement is basically like a cin >> number statement, except instead of cin (which is from the keyboard input) you are using myfile as your input.
you have a very simple case, since each myfile >> input_from_file command reads up to a SPACE (i.e. " ") or a new line (i.e. "\n"). this is good for you because your numbers are already separated by spaces!
then from your cin >> number statement, you can just simply use
|
cout << "The difference is " << number - input_from_file;
|
if that doesn't work, you can convert a string (i know it works with character arrays, i think it should for strings too) containing numbers with the command
1 2
|
int convert;
convert = atoi(stringName);
|
atoi stands for "ASCII to Integer" (ASCII would be a character).
if you use the second strategy, you can't use the "getline" function because it does exactly what it says - it gets the ENTIRE line, and stores it as a string (that means all the numbers, the spaces, and the new line character "\n"). i think you need to either just use
1 2 3
|
myfile >> string;
//or
myfile.getline(line, 50, ' ');
|
the last two arguments in the getline() function means read the getline function will stop either if it has read 50 characters or if it encountered a SPACE.
i hope that answers your question