Struggling with Reading File Input

Hello,

I am working on a school project that requires me to read basketball statistics from a file named in06.txt. The file in06.txt looks exactly as follows:

5
P 17 24 9 31 28
R 4 5 1 10 7
A 9 2 3 6 8
S 3 4 0 5 4

I am required to read and store the first number, 5, into a variable called "games." From there, I must read the numbers from the second line and determine the high, the low, and the average. I must do the same thing for lines 3, 4, and 5. (FYI, the letters P, R, A, and S are there to indicate "Points," "Rebounds," "Assists," and "Steals.")

Before I overwhelm myself by jumping right into dealing with every aspect of the project (I only have a few weeks of experience with C++ and this is a large step up from our last assigned project) I am working on determining the average from each line. My plan is to keep a running total of each line and then divide the running total by the number of games, which is 5 in this case.

My code looks as follows:
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
	int games;
    	int points_high, points_low, points_total;
	int rebounds_high, rebounds_low, rebounds_total;
	int assists_high, assists_low, assists_total;
	int steals_high, steals_low, steals_total;
	double points_average, rebounds_average, assists_average, steals_average;
	
	ifstream fin;
	ofstream fout;

	fin.open("in06.txt");
		if( fin.fail() ) {
			cout << "\nInput file opening failed.\n";
			exit(1);
		}
		else
			cout << "\nInput file was read successfully.\n";

	int tempint1, tempint2, tempint3, tempint4;
	char tempchar;

	fin >> games;
	fin.get(tempchar);	// Takes the endl; from the text file.
	fin.get(tempchar);	// Takes the character P from the text file.
	
	while( fin >> tempint1 ) {
		 points_total += tempint1;
	}

	fin.get(tempchar);	// Takes the endl; from the text file.
	fin.get(tempchar);	// Takes the character R from the text file.

	while( fin >> tempint2 ) {
	 	rebounds_total += tempint2;
	}
	
	fin.get(tempchar);	// Takes the endl; from the text file.
	fin.get(tempchar);	// Takes the character A from the text file.

	while( fin >> tempint3 ) {
		 assists_total += tempint3;
	}

	fin.get(tempchar);	 // Takes the endl; from the text file.
	fin.get(tempchar);	 // Takes the character S from the text file.
	
 	while( fin >> tempint4 ) {
		 steals_total += tempint4;
	}

	cout << "The total number of games is " << games << endl;
	cout << "The value of total points is " << points_total << endl;
	cout << "The value of total rebounds is " << rebounds_total << endl;
	cout << "The value of total assists is " << assists_total << endl;
	cout << "The value of total steals is " << steals_total << endl;

	return 0;
}


(I added the cout messages so that I can keep track of what is happening in my program.)

After I compile and run my program, I get the following output:

Input file was read successfully.
The total number of games is 5
The value of total points is 111
The value of total rebounds is 134522076
The value of total assists is 134515888
The value of total steals is 673677934


I have been reading about file input in my textbook for hours now trying to find something that may help me figure out why my program does not do what I want it to do, but I have had no luck.

Also, my teacher does not want us to store every integer from the input file. He only wants us to store the number of games and then use a loop and if statements for the rest of the project.

If anyone could provide me with any advice or assistance, I would greatly appreciate it.

Thanks!
Last edited on
Topic archived. No new replies allowed.