Help with files and counter

This code is suppose to read a file with student ids and their test scores which then gets an average and put all the information in another file. The problem is that the counter seems to add the last test score twice once it hits 0000 and make the total divide by 21 instead of 20. Also no output file is made.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

void readscore (int &, int &, int &, int &, int, ifstream &);
void printscore (int , int , int, int);


int main()
{
	int id, score, num, ave, test;
	ifstream data ("test_scores.txt");
	readscore (id,test,num,ave,score,data);
	printscore(id,score,num,ave);
	
	return 0;
}

void readscore (int &id, int &test, int &num, int &ave, int score, ifstream &data)
{
	test = 0;
	if (!data)
		cout << "Error File Does Not Exist, Please Try Again" << endl;
	else
		num = 0;
		do
		{
			num = num + 1;
			data >> id >> score;
			test += score;
			cout << num << " " << id << " " << test << endl; // to check # of students and addition *will remove later
		}
		while ( id != 0);
	
	ave = test/num;
	data.close();
}

void printscore (int id,int score, int num,int ave)
{
	
	ifstream data ("test_scores.txt");
	cout << "\n\t\tSCIENCE DEPARTMENT FALL 2018"<< endl;
	cout << "\t\t  TEST ANALYSIS\n"<< endl;
	cout << "\t\tThe class average is " << ave << endl;
	for(int num = -1; id !=0;num++)
	{
	void filewrite(int &, int &,int &,int &, ifstream &);
	filewrite(id,score,num,ave,data);
	}
}
void filewrite(int &id, int &score,int &ave,int &num, ifstream &data)
{
	string msg;
	ofstream fout;
	fout.open("studentlist.txt");
	
	data >> id >> score;
	cout << "\nStudent  Test Score  Message"<< endl;
	if (score > ave)
		msg = "above average";
	else if (score == ave)
		msg = "is equal to average";
	else
		msg = "below average";
	fout << num <<" "<< id <<" "<< score<< " " << msg << endl;
	fout.close();
}
Last edited on
Let's look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void readscore (int &id, int &test, int &num, int &ave, int score, ifstream &data)
{
	test = 0;
	if (!data)
		cout << "Error File Does Not Exist, Please Try Again" << endl;
	else
		num = 0;
		do
		{
			num = num + 1;
			data >> id >> score;
			test += score;
			cout << num << " " << id << " " << test << endl; // to check # of students and addition *will remove later
		}
		while ( id != 0);
	
	ave = test/num;
	data.close();
}


First do you realize that your indentation is not correct?

This is what it really should look like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void readscore (int &id, int &test, int &num, int &ave, int score, ifstream &data)
{
    test = 0;
    if (!data)
        cout << "Error File Does Not Exist, Please Try Again" << endl;
    else
        num = 0;
    do
    {
        num = num + 1;
        data >> id >> score;
        test += score;
        cout << num << " " << id << " " << test << endl; // to check # of students and addition *will remove later
    } while ( id != 0);

    ave = test/num;
    data.close();
}


Looking at this code I would expect that you would have one extra record in your data set since you check for the terminator after you have done the increment and all the other calculations.

Also you really shouldn't be closing the data file in this function, or even testing if the file is open correctly, these operations should be done by the function that is opening the file. No real reason to even venture into this function if the file failed to properly open.


Topic archived. No new replies allowed.