Why printing to file like this?

Finally got this program to do something besides spin my data drive around in infinite debugging mode. Problem is the output is hit and miss.

It probably a very simple problem I'm not seeing. Code crackers please take a look at program, output, and input file and throw down a life jacket!

Here's the program:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <conio.h>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Prototypes

void Initialize(int&, double&);
void GetData(ifstream&, ofstream&, string&, int&, int&, int&, int&, int&);
void FindLowest(int&, int&, int&, int&, int&, int&);
void CalcAverage(int&, int&, int&, int&, int&, int&, double&);
void PrintResults(ofstream&, string&, int&, int&, int&, int&, int&, double&);
void AccumulateClassInfo(ifstream&, int&, double&, double&);
void CalcClassAverage(int&, double&, double&);
void PrintClassInfo(ofstream&, int&, double&);

int main()
{
     ifstream inData;           // input file
     ofstream outData;          // output file
     string studentID;          // student ID number
     int numStudents;           // number of students in file
     int t1;                    // test 1 score
     int t2;                    // test 2 score
     int t3;                    // test 3 score
     int t4;                    // test 4 score
     int t5;                    // test 5 score
     int lowestTest;            // lowest test score
     double average;            // average of 4 highest tests
     double sumOfAverages;      // sum of the averages of all students
     double classAverage;       // average for class

     inData.open("a:\\studentinfo.txt");       
     outData.open("a:\\studentoutput.txt");

	outData << fixed << showpoint;

		outData << "Student ID   " << "Test 1   " << "Test 2   "
			<< "Test 3   " << "Test 4   " << "Test 5   " <<
			"Average" << endl << endl;

	 //Function Calls

	 Initialize(numStudents, sumOfAverages);

	 GetData(inData, outData, studentID, t1, t2, t3, t4, t5);
		 while (inData)
		 {
			 if (!inData)
			 {
				 cout << "Data error:  File not found." << endl;
				 cout << "Program terminates" << endl;
				 return 1;
			 }
			 else
			 {
			FindLowest(t1, t2, t3, t4, t5, lowestTest);

			CalcAverage(t1, t2, t3, t4, t5, lowestTest, average);

			PrintResults(outData, studentID, t1, t2, t3, t4, t5, average);
	
			AccumulateClassInfo(inData, numStudents, average,
				sumOfAverages);

			
			}
			 GetData(inData, outData, studentID, t1, t2, t3, t4, t5);
			 outData << endl;
		 }  // End while loop

		 
	CalcClassAverage(numStudents, sumOfAverages, classAverage);

	PrintClassInfo(outData, numStudents, classAverage);

	


	 inData.close();
	 outData.close();

	 getch();
	 return 0;
}

//Function Definitions

void Initialize(int& sNum, double& sumAvg)
{
	sNum = 0;
	sumAvg = 0.00;
}

void GetData(ifstream& inPut, ofstream& outPut, string& sID, int& test1,
	int& test2, int& test3, int& test4, int& test5)
	{
		inPut >> sID >> test1 >> test2 >> test3 >> test4 >> test5;
		
	}

	void FindLowest(int& test1, int& test2, int& test3, int& test4,
		int& test5, int& lowScore)
	{
		if ((test1 < test2) && (test1 < test3) && (test1 < test4)
			&& (test1 < test5))
			lowScore = test1;
	else if ((test2 < test1) && (test2 < test3) && (test2 < test4)
			&& (test2 < test5))
			lowScore = test2;
	else if ((test3 < test1) && (test3 < test2) && (test3 < test4)
			&& (test3 < test5))
			lowScore = test3;
	else if ((test4 < test1) && (test4 < test2) && (test4 < test3)
			&& (test4 < test5))
			lowScore = test4;
	else
		lowScore = test5;
	}

	void CalcAverage(int& test1, int& test2, int& test3, int& test4,
		int& test5, int& lowScore, double& avg)
	{
		if (test1 = lowScore)
			avg = (test2 + test3 + test4 + test5) / 4;
		else if (test2 = lowScore)
			avg = (test1 + test3 + test4 + test5) / 4;
		else if (test3 = lowScore)
			avg = (test1 + test2 + test4 + test5) / 4;
		else if (test4 = lowScore)
			avg = (test1 + test2 + test3 + test5) / 4;
		else
			avg = (test1 + test2 + test3 + test4) /4;
	}

	void PrintResults(ofstream& outPut, string& sID, int& test1, int& test2,
		int& test3, int& test4, int& test5, double& avg)
	{
		

		outPut << sID << "   " << test1 <<
			"   " << test2 << "   " << test3 << "   "
			<< test4 << "   " << test5 << "   " <<
			setprecision(2) << avg << endl;
	}

	void AccumulateClassInfo(ifstream& inPut, int& sNum,
		double& avg, double& sumAvg)
	{
		sNum++;
		avg++;
		sumAvg = (sumAvg + avg) / sNum;
	}

	void CalcClassAverage(int& sNum, double& sumAvg, double& classAvg)
	{
		classAvg = sumAvg / sNum;
	}

	void PrintClassInfo(ofstream& outPut, int& sNum, double& classAvg)
	{
		outPut << setw(15) << "Number of Students" << setw(30) <<
			"Class Average" << endl << endl;

		outPut << setw(15) << sNum << setw(30) << setprecision(2)
			<< classAvg << endl;
	}


Here's the input file:

123 90 98 80 79 70
234 87 89 67 90 77
345 57 68 79 80 88
456 65 54 60 70 65
567 70 71 72 80 75
678 80 87 85 90 88
789 70 75 80 62 92
890 40 50 60 70 80
901 90 95 92 96 100

Here is the output file: (Output not aligned yet.).

Student ID   Test 1   Test 2   Test 3   Test 4   Test 5   Average

123   70   98   80   79   70   81.00

234   67   89   67   90   77   80.00

345   57   68   79   80   88   78.00

456   54   54   60   70   65   62.00

567   70   71   72   80   75   74.00

678   80   87   85   90   88   87.00

789   62   75   80   62   92   77.00

890   40   50   60   70   80   65.00

901   90   95   92   96   100   95.00

Number of Students                 Class Average

              9                          1.31


Right now I'm just trying to get it to read the student id/test/average lines. See the pattern on output is like this:
line 1 - error
line 2 - error
line 3 - ok
line 4 - error
line 5 - ok
line 6 - ok
line 7 - error
line 8 - ok
line 9 - ok

Will you guys tell me wheres the bad code? I know class average is messed up.

the project is to write every thing with void functions.

Topic archived. No new replies allowed.