Input from a .txt file

The assignment is this:

I have been given a .txt files which contains 4 lines of data. Each line having 5 numbers.
The program should take take each number (starting from the first line and moving towards
the right and then down, we arent allowed to rearrange the numbers on a straight line),
and subtract it from a number input from the user and display
the result of each. The numbers are this:


22000 43345 2343 234234
2343 2343 234343 1232
9802349 2342 189546 903458
34598345 9904358 3245324 345345 896843823452345

Uptil now heres the code i have written. How do i read one object after the other?
and go about it?



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  int number;
  int diff;

  cout<< "Please enter your number:\n";
 cin >> number;


  ifstream myfile ("example2.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
	   

    }
    myfile.close();
  }

  else cout << "Unable to open file"; 
  
  cin.get();

  return 0;
}

Last edited on
any help will be greatly appreciated.
use the third argument of getline as ' '.
i.e instead of
getline (myfile,line);
use
getline (myfile,line, ' ');
Thanks for the reply.

That does help, but its not quite what i want. I need to put the numbers one by one in a new variable (lets call it number2, and subtract it from number, putting the result in diff and displaying that)

So, i still dont know how to 'read' one number after the other, although now i can print them out.
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
Last edited on
It does. Thank you for the help.
Topic archived. No new replies allowed.