Figuring out an issue with an output file

hello everyone. Ive been working on this program for a class and ive got it working for the most part but there is an issue with the output i cant quite figure out whats going wrong. The program correctly takes all the data from the input file but when the output file is written there is some random numbers appearing at the end displaying incorrect results. Can anyone help me figure out the problem? ill include the data from the input and output files along with my code. I appreciate anyone who takes the time to help.

/////input file
1234 52 70 75
2134 90 76 90
3124 90 95 98
4532 21 17 81
5678 20 22 45
6134 34 45 55
7874 60 99 56
8026 70 10 66
9893 34 09 77
2233 78 20 78
1947 45 40 88
3456 78 55 78
2877 55 50 95
3189 70 98 78
2132 77 97 80
4602 89 50 91
3445 78 60 78
5405 35 33 15
4556 78 20 18
6999 88 98 89
9898 48 78 68
2323 78 20 78

//////
this is what is outputting

1234 52 70 75 65.67
2134 90 76 90 85.33
3124 90 95 98 94.33
4532 21 17 81 39.67
5678 20 22 45 29.00
6134 34 45 55 44.67
7874 60 99 56 71.67
8026 70 10 66 48.67
9893 34 9 77 40.00
2233 78 20 78 58.67
1947 45 40 88 57.67
3456 78 55 78 70.33
2877 55 50 95 66.67
3189 70 98 78 82.00
2132 77 97 80 84.67
4602 89 50 91 76.67
3445 78 60 78 72.00
5405 35 33 15 27.67
4556 78 20 18 38.67
6999 88 98 89 91.67
9898 48 78 68 64.67
2323 78 20 78 58.67
353 3 11016464 3 3672156.67
High Score : 90 11016464 98
Low Score : 3 9 3
Quiz Average: 59.61 479027.22 68.70


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  #include <iostream>
#include <iomanip>
#include <fstream>



using namespace std;


void findstudentavg(int quiz1[], int quiz2[], int quiz3[], double avg[], int n)
	{
		for(int i = 0; i < n; i++)
			avg[i] = (quiz1[i] + quiz2[i] + quiz3[i]) / 3.0;
	}
	
	
int findlow(int array[], int n)
	{
		int low = array[0];
		for(int i = 1; i < n; i++)
		if(array[i] < low)
		low = array[i];
		return low;
	}
	
	
int findhigh(int array[], int n)
	{
		int high = array[0];
		for(int i = 1; i < n; i++)
		if(array[i] > high)
		high = array[i];
		return high;
	}
	
	
double findqzavg(int quiz[], int n)
	{
		double sum = 0.0;
		for(int i = 0; i < n; i++)
		sum += quiz[i];
		return sum / n;
	}
	
int getdata(ifstream &fin, int studentID[], int quiz1[], int quiz2[], int quiz3[])
	{
		int count = 0;
		while(true)
		{
			fin>>studentID[count];
			if(studentID[count] == 0)
				return count;

			fin>>quiz1[count]>>quiz2[count]>>quiz3[count];
			count++;
		}
	}
void printall(ofstream &fout, int studentID[], int quiz1[], int quiz2[], int quiz3[], double avg[], int n)
	{
		for(int i = 0; i < n; i++)
			fout<<studentID[i]<<"\t"<<quiz1[i]<<"\t"<<quiz2[i]<<"\t"<<quiz3[i]<<"\t"<<fixed<<setprecision(2)<<avg[i]<<endl;
			
		fout<<"High Score : "<<findhigh(quiz1, n)<<"\t"<<findhigh(quiz2, n)<<"\t"<<findhigh(quiz3, n)<<endl;
		fout<<"Low Score : "<<findlow(quiz1, n)<<"\t"<<findlow(quiz2, n)<<"\t"<<findlow(quiz3, n)<<endl;
		fout<<"Quiz Average: "<<findqzavg(quiz1, n)<<"\t"<<findqzavg(quiz2, n)<<"\t"<<findqzavg(quiz3, n)<<endl;
	
	}
	
int main()
{
	ifstream fin;
	ofstream fout;
	fin.open("pr2data.txt");
	fout.open("outData.txt");
	
	if(!fin.is_open())
	{
		cout<<"Unable to open the file."<<endl;
		return 0;
	}
	
	int studentID[30], quiz1[30], quiz2[30], quiz3[30];
	double studentAvg[30];
	int count = getdata(fin, studentID, quiz1, quiz2, quiz3);
	findstudentavg(quiz1, quiz2, quiz3, studentAvg, count);
	printall(fout, studentID, quiz1, quiz2, quiz3, studentAvg, count);
}
It seems to work for me. Try controlling your input loop like this:

1
2
3
4
5
6
7
8
9
10
int getdata(ifstream &fin, int studentID[], int quiz1[], int quiz2[], int quiz3[])
	{
		int count = 0;
		while(fin>>studentID[count])
		{
			fin>>quiz1[count]>>quiz2[count]>>quiz3[count];
			count++;
		}
		return count;
	}

I don't believe the C++ standard says that if fin >> n fails (and n is an integer!) then n will be set to 0. It actually seems to keep whatever it's last value was. The proper way to control a loop in most cases is to test if the read operation failed. Believe it or not, you don't even want to check fin.eof() since it can be set even though the read operation hasn't failed (i.e., it will have read something that you probably want to process).
Last edited on
50
51
52
			fin>>studentID[count];
			if(studentID[count] == 0) //¿are you sure that this is how it works?
				return count;
Ah perfect that was a much better approach to the loop than what i had. Thank you both for the response and for the much needed insight!
Topic archived. No new replies allowed.