OK. I am supposed to write a program for averaging quiz grades. The problem I am having is the last nested loop. It is supposed to output averages for three students and the whole class average. currently it is displaying the numbers correctly but it is displaying 3 extra sets based on the last line of the input file. I am really lost and any help is ver much appreciated. below is my code and instructions.
1. Use a nested loop structure to input the data and Write the data to a text file.
a. The outer loop will be a while loop; the inner loop will be a for loop (4 quizzes).
b. Validate whether the quiz scores are in range (0-100).
c. Since you do not know how many students will be entered, add a way to quit the loop.
d. Add spaces in between each item added to the text file. Add a new line after each student. The text file that your program creates will look like the following
2. Use a nested loop structure to read the data from the text file and calculate the student’s average grade.
a. The outer look will be a while loop; the inner loop will be a for loop (4 quizzes)
b. To calculate each student’s average score, use a total variable initialized to 0 before the for loop, then calculate the student’s average after the loop.
c. To calculate the class average, initialize a classTotal variable to 0 before the while loop, add each student’s total into the classTotal following the for loop, then calculate the classAverage after the while loop.
d. Only display 2 decimals for the averages.
3. BEWARE of integer division.
4. Use cout to output the values of the variables to the console. Your cout statement MUST use the variables to display the values
5. Output must be labelled and easy to read as shown in the sample output below.
#include <iostream> // used for cout and cin //
#include <fstream> // used for ofstream and ifstream //
#include <iomanip> // used for setprecision
using namespace std;
int main()
{
int studentID, // variables to hold student ID and quiz grades //
quizGrade1,
quizGrade2,
quizGrade3,
quizGrade4,
choice = 1; // varaible initiated at one to control while loop //
ofstream outputFile; // named output file //
outputFile.open("C:\\Users\\Mantle\\Desktop\\CIS 1111\\quizgrades.txt"); // opens path to ouput file //
cout << "Enter 0 for no students" << endl;
while (choice) // while loop to allow the user to control students entered //
{
for (int quiz = 0; quiz < 4; quiz++) // for loop to have user enter 4 quiz scores per student. Will ask user to re-enter if input is invalid //
{
cout << "Enter the student ID:" << endl;
cin >> studentID;
if (studentID == 0)
break;
cout << "Enter quiz grade: ";
cin >> quizGrade1;
while (quizGrade1 < 0 || quizGrade1 > 100)
{
cout << "ERROR: Please re-enter grade between 0 & 100: ";
cin >> quizGrade1;
}
cout << "Enter quiz grade: ";
cin >> quizGrade2;
while (quizGrade2 < 0 || quizGrade2 > 100)
{
cout << "ERROR: Please re-enter grade between 0 & 100: ";
cin >> quizGrade2;
}
cout << "Enter quiz grade: ";
cin >> quizGrade3;
while (quizGrade3 < 0 || quizGrade3 > 100)
{
cout << "ERROR: Please re-enter grade between 0 & 100: ";
cin >> quizGrade3;
}
cout << "Enter quiz grade: ";
cin >> quizGrade4;
while (quizGrade4 < 0 || quizGrade4 > 100)
{
cout << "ERROR: Please re-enter grade between 0 & 100: ";
cin >> quizGrade4;
}
outputFile << studentID << " " << quizGrade1 << " " << quizGrade2 << " " << quizGrade3 << " " << quizGrade4 << endl; // sends user entered info to output file //
cout << endl << "Enter 0 for no more students. Enter 1 for more students." << endl << endl;
cin >> choice; // ask user to enter more students or not and uses break to break the loop if usedr enters 0 //
if (choice == 0)
break;
}
}
outputFile.close(); // closes output file //
// THIS IS WHERE I START HAVING PROBLEMS. THE TOTALS SEEM TO BE ADDING UP CORRECTLY AND THE AVERAGES ARE WORKING FOR STUDENTS BUT IT
// IS DISPLAYING 3 EXTRA SETS OF AVERAGES FROM THE THIRD LINE IN THE DATA FILE. //
double classTotal = 0.0; // variable to hold class total initiated at 0 //
while (inputFile) // while loop used for input file //
{
double studentTotal = 0.0; // variable to hold student total quiz grades initiated at 0 //
for (int count = 1; count <= 3; count++) // for loop used to read quiz grades for students entered above //
{
inputFile >> studentID; // reads student ID from input file //
inputFile >> quizGrade1 >> quizGrade2 >> quizGrade3 >> quizGrade4; // reads quiz grades from input file //
cout << quizGrade1 << " " << quizGrade2 << " " << quizGrade3 << " " << quizGrade4 << endl; // not needed. I was using it to make sure quiz grades were correct /
studentTotal = (quizGrade1 + quizGrade2 + quizGrade3 + quizGrade4); // calculates student total quiz score //
cout << studentTotal << endl; // not needed. I was using it to make sure totals were correct
classTotal += studentTotal; // adds student totals to class total //
double studentAverage = studentTotal / 4; /// calculates average for student quiz grades //
cout << "Student average score is: " << studentAverage << endl; // displays student average //
}
}
inputFile.close(); // closes input file //
cout << classTotal; // not needed. used to see if class total was adding up correctly.
Try changing this loop, change for (int count = 1; count <= 3; count++) to for (int count = 1; count <= 127; count++)[/code] and my guess is you'll get 127 answers.
(Removing the loop altogether might be a good move because you only need to read the file once. :)
ha ha.... I want the loop to show three outputs for three students but the loop shows three students correctly as it should... then it shows three more outputs that use the numbers from the third read. the nested loop is required.
I guess I thought you were being a smart ass... sorry. I have to keep the loop so if you could help me I would appreciate it. I am pretty new at this and have been working on this for quite a while and am about to lose my mind.
Your earlier while inputFile is OK but the loop inside it is duplicating the effort.
I haven't seen your data file but my guess from the comments you have made is you have 3 students on the file.
What you should be doing is writing some pseudocode to concentrate your mind (or what's left of it) on what you are trying to do:
eg
1. Set class statistics to zero
2. Open the file:
3. read a line
4. process the results (averages) for that student
4a. print student results
4b. update no. of students
4b. upgrade the class totals
5. Repeat loop starting at 3 and while no more students available on file
6. Calculate class totals and averages based on student count
7. Print out class results
The pseudocode items can form the basis for your program comments, not the ones you have which just unnecessarily repeat the code statements (which is very bad practice)
This is the correct way to read the file. It will stop reading when the data runs out, assuming you data file doesn't have errors, ie not compatible with the number of inputs and/or they are of the wrong type but leave that aside.