reading different data values from file

Hi. I'm working on a problem where you read data from a file, accumulate a total, and then output the data and total. The data looks something like this:

7 21 361 John Smith 48 19.95


The problem is that when the data is output, it outputs the string part as a 0 and never generates the numbers after that. Also, it creates an infinite loop of those first three numbers and never goes to the next line. I know it has something to do with the string, but I don't know what to do when the string is in the middle of the line and there are different data values before and after it.

1
2
3
4
5
6

	inFile.open("filename.txt");
	if(inFile.fail())
             cout << "Error opening file.\n";
        else
	     inFile >> num1 >> num2 >> num3 >> charName >> num4 >> num5;


I have charName as a character array right now; I don't know too much about using string. I'm still very much a beginner, so simple language, please.
When reading strings, the >> formatted input operator stops
reading the string when it encounters a white space character.
Spaces, tabs and carriage returns are all white space characters.

This means that John Smith is actually 2 strings.

So you should be reading the line as follows:
int int int string string int float inFile >> num1 >> num2 >> num3 >> charName1 >> charName2 >> num4 >> num5

trivial example to read first line of file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  int num1=0,num2=0,num3=0,num4=0;
  float num5 =0.0;
  char firstName[100], lastName[100]; //each array should be large enough to hold longest possible names
  
  ifstream inFile;

  inFile.open("filename.txt");
	if(inFile.fail())
    cout << "Error opening file.\n";
  else
  {
    inFile >> num1 >> num2 >> num3 >> firstName >> lastName >> num4 >> num5;
    
    //display read values
    cout<< num1 << "  " << num2 << "  " << num3 << "  " 
      << firstName << "  " << lastName << "  " << num4 << "  " << num5;
    
    inFile.close();//close the file

  }
Topic archived. No new replies allowed.