Validating loop input and writing to file

I have to write a program that uses files and nested loops to create a file from the quiz grades entered by the user, then read the grades from the file and calculates the average quiz grade for a class. Each student takes 4 quizzes. Use a nested loop to write each students quizzes to a file. Then read the data from the file in order to display the student's average score and the class average.

The problem I am having is validating test scores and having them write to the file. I do not want the test score to be >100. I have tried several different methods of validating but cant seem to find the correct way to do it inside the loop.

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
//Cassandra Hamric
//30 March 2016
//This program claculates average quiz grades for a class.

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
int main()
{
	
 ofstream outfile;
 outfile.open("quizgrades.txt"); // Open a file named quizgrades.txt
 int studentID,grades,choice=1;

 while(choice!=0)
 {
     cout<<"Enter the student id : "<<endl; // Get student ID
     cin>>studentID;
	 cout <<endl;

     outfile<<studentID<<" "; // Send student ID to file
     for(int id=0;id<4;id++)
     {
         cout<<"Enter quiz grades : "; //get quiz grades
         cin>>grades;
         outfile<<grades<<" "; //send grades to file

		 if (grades > 100) //Validate quiz scores are not above 100.
			 cout <<"Error! Grade must not be above 100. Please re-enter grades.";
		 cin >> grades;
		 outfile <<grades << " ";
	 }
	 
	

     outfile<<endl;
     cout<<"\nEnter 0 for no more students to enter. Enter 1 for more students."<<endl; // Choose if more students to enter
     cin>>choice;
 }
 outfile.close(); //close outfile


 ifstream infile;
 infile.open("quizgrades.txt"); // Open the file
 int count=1;
 int classTotal=0;
 while(!infile.eof())
 {
     infile>>studentID; //Read the student ID's
     int total=0;
     for(int id=0;id<4;id++)
     {
         infile>>grades; // Read the student grades
         total+=grades;
     }
    double average= total/4.0; // calculate average score
     classTotal+=total;
     cout<<"Student "<<count<<" average score is : "<<average<<endl;
     count++;
     char ch;
     infile>>ch;
 }
 count--;
 float netAverage= float(classTotal)/(count*4);
  cout.precision(4);
  cout<<"Average quiz score for the class is "<<netAverage <<endl;

  system ("pause"); 
 return 0;

}
Last edited on
So what do you want to have happen if it is true that the grade is greater than 100? The logic looks correct. If you are wanting to re-prompt, you need to stick the cin in some sort of loop when you are re-prompting for the grade.
It doesn't work. The program will ask for like 8 grades as it is. I changed it to
1
2
3
4
5
6
7
	 	while (grades > 100)
		{
			cout << "\n Grade must not be over 100"<<endl; // If Start is Greater than End, Output Error
			cout <<"Enter quiz grades : " ;
			cin >> grades;
			 outfile<<grades<<" "; //send grades to file
		}

And that does not work either, it will keep the first grade of 200 and copy that to the text file. lf the grade is over 100, I want it to re-start the input of that particular students grades.
Your logic is wrong.
1
2
3
outfile<<grades<<" "; //send grades to file
if (grades > 100) //Validate quiz scores are not above 100.
  cout <<"Error! Grade must not be above 100. Please re-enter grades.";


You need to check the grade before you write it to the file.

I would use a separate function to get the grade:
1
2
3
4
5
6
7
8
9
10
11
12
13
int GetGrade()
{
  bool valid = false;
  int grade = 0;

  while (!valid)
  {
    cout << "Enter grade (max 100): ";
    cin >> grade;
    if (grade <= 100)
      return grade;   
  }
}
Topic archived. No new replies allowed.