assistance with input file not working

My console output is all jacked up; my averages are way off and the stuID are not accurate. although the output file got written to fine.


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
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
	int numStudents, numQuizzes = 4, studentCount = 1, count = 1, quiz = 1, score = 0, quizCount = 1;
	int stuID;
	double average;
	double total = 0;
	ofstream outputFile;
	ifstream inputFile;

	cout << fixed << setprecision(2);

	//Open file scores.txt
	outputFile.open("quizgrades.txt");

	//Get the number of students
	cout << "How many students? ";
	cin >> numStudents;


	while (studentCount <= 3)
	{
		cout << "Enter Student ID: ";
		cin >> stuID;
		outputFile << stuID << " ";

		for (quiz = 1; quiz <= numQuizzes; quiz++)
		{

			cout << "Enter grade for quiz " << quiz << ": ";
			cin >> score;
			outputFile << score << " ";
		}
		outputFile << endl;
		studentCount++;

	}

	outputFile.close(); //closes Output File

	inputFile.open("quizgrades.txt"); //opens Output File

	studentCount = 1;
	while (studentCount <= 3)
	{

		for (quiz = 1; quiz <= numQuizzes; quiz++)
		{
			inputFile >> score;
			total += score;
		}

		average = total / 4;
		inputFile >> stuID;
		cout << "The average for student " << stuID << " is " << average << endl;

		studentCount++;
	}

	inputFile.close(); //closes Input File





	system("pause");
	return 0;
}


How many students? 3
Enter Student ID: 1001
Enter grade for quiz 1: 100
Enter grade for quiz 2: 10
Enter grade for quiz 3: 100
Enter grade for quiz 4: 100
Enter Student ID: 1002
Enter grade for quiz 1: 100
Enter grade for quiz 2: 50
Enter grade for quiz 3: 60
Enter grade for quiz 4: 60
Enter Student ID: 1003
Enter grade for quiz 1: 30
Enter grade for quiz 2: 40
Enter grade for quiz 3: 50
Enter grade for quiz 4: 60
The average for student 100 is 302.75
The average for student 60 is 605.75
The average for student 60 is 886.50
Last edited on
You never reset average to 0 after working with it.
I wasn't sure what you meant so I put it after line 55.
It seems to be working a little more accurately, but not exact.
Also, the output of the students is still wrong. Any reason why it keeps thinking the stuID is the last quiz score entered?
Sorry, I meant total - I looked at the wrong variable by mistake.
Topic archived. No new replies allowed.