I was instructed to create a program that uses nested loops to get 4 quiz scores from any amount of students, output them into a file, and then input them back into the program from the file and get the average for each student and the class, however when I get my input back from the file, it is producing weird results.
Here is my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
usingnamespace std;
int main()
{
string test;
int studentID, quiz1, quiz2, quiz3, quiz4, quiz_total, choice, go;
cout << "Enter 0 for no students or 1 to enter students" << endl;
cin >> go;
while (go == 1) {
for (int i = 0; i < 1; i++)
{
cout << "Enter student id\n";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
while (quiz1 < 0 || quiz1 > 100)
{
cout << "\nError in entry...Enter a valid test score 1-100>";
cin >> quiz1;
}
cout << "Enter quiz grade 2: ";
cin >> quiz2;
while (quiz2 < 0 || quiz2 > 100)
{
cout << "\nError in entry...Enter a valid test score 1-100>";
cin >> quiz2;
}
cout << "Enter quiz grade 3: ";
cin >> quiz3;
while (quiz3 < 0 || quiz3 > 100)
{
cout << "\nError in entry...Enter a valid test score 1-100>";
cin >> quiz3;
}
cout << "Enter quiz grade 4: ";
cin >> quiz4;
while (quiz4 < 0 || quiz4 > 100)
{
cout << "\nError in entry...Enter a valid test score 1-100>";
cin >> quiz4;
}
std::fstream outputFile;
outputFile.open ("quizgrades3.txt", std::fstream::in | std::fstream::out | std::fstream::app);
if (outputFile.is_open())
{
outputFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4 << "\n";
outputFile.close();
}
else
{
cout << "Error opening file!\n";
}
}
cout << "\nEnter 0 for no more students to enter. Enter 1 for more students.";
cin >> go;
}
double totalStudentQuizzes = 0.0;
double totalClassQuizzes = 0.0;
double averageQuiz;
int nuStudents = 0;
int studentNum;
ifstream inFile;
inFile.open("quizgrades3.txt");
while (inFile >> quiz1) {
for (int t = 1; t < 3; t++) {
//read the numbers from the file
inFile >> studentNum;
inFile >> quiz1;
inFile >> quiz2;
inFile >> quiz3;
inFile >> quiz4;
totalStudentQuizzes = quiz1 + quiz2 + quiz3 + quiz4;
totalClassQuizzes = totalClassQuizzes + totalStudentQuizzes;
nuStudents++;
cout << "Student #: "<< studentNum << "\n"
<< "Student Test Scores:" << quiz1 << ", " << quiz2 << ", " << quiz3 << ", and " << quiz4 << "\n";
}
averageQuiz = totalClassQuizzes / nuStudents;
cout << "\nAverage quiz score is "<< averageQuiz<<endl;
}inFile.close();
system("pause");
return 0;
}
When I look in my code in the file, it is formatted exactly how it is supposed to be, with the student # first and a space between each quiz grade then a line break after each separate student. However my results are weird and seem to be skipping the first student number all together, which is throwing all the numbers off.
Ex. lists the student id as 90 and the first students test scores as: 100, 80, 100, and 1001(this 1001 number is the second students ID...)
Maybe it has something to do with the way the information is output into the file? I'm not sure.
When I look in my code in the file, it is formatted exactly how it is supposed to be, with the student # first and a space between each quiz grade then a line break after each separate student.
Then maybe you should read it as if that is the way it is formatted. The first thing you read from the file is a quiz score (which you then discard.)
I was instructed to create a nested loop for that part: a for loop inside of a while loop. That while statement on line 77 is supposed to keep the loop running until there is nothing left in the file for it to read....
Perhaps I did that part wrong? That could be the cause for the skewed numbers.
This is what I input when I run through the program: Student id Quiz 1 Quiz 2 Quiz 3 Quiz 4
1000 90 100 80 100
1001 85 95 85 100
1002 50 60 70 100
and this is the result:
Student #: 90
Student test scores: 100, 80, 100, and 1001
Student #: 85
Student test scores: 95, 85, 100, and 1002
Average quiz score is 1281.5
Student #: 60
Student test scores: 70, 100, 100 and 1002
Student #: 60
Student test scores: 70, 100, 100 and 1002
Average quiz score is 1276.75
So as you can see, the numbers are all messed up. Not only that, but it lists it for 4 students when only 3 were entered. Also, where did the 50 go that I output into the file? It never shows up in my output?
Also, why does the loop output the average quiz score twice?
The problem on line 77 is that inFile >> quiz1 reads the first value (which is 1000: the first student id). Line 82 will read Quiz 1 (hence 'Student #: 90') and so on.
This way the nested loop doesn't make sense. The only reason for a nested loop I can think of is that instead of the variables quiz1 ... quiz4 you should only have one (quiz) and read/write this with a loop.
Yeah...if I didn't have to have a nested loop, I wouldn't.
But, this was my instruction for the second nested loop:
2.Use a nested loop structure to read the data from the text file and calculate the student’s average grade.
a. The outer loop 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
By the way, thank you for all your help so far coder777!
I'll try to use just one variable (quiz) and see if it works out.
while (inFile >> studentNum) {
int totalStudentQuizzes = 0;
for (int t = 0; t < 3; t++) { // Note: 0...3 -> 4 loops
//read the quiz from the file
int quiz;
inFile >> quiz;
cout << quiz;
totalStudentQuizzes += quiz;
}
AHHH. Thank you so much!!!
I changed the for loop to this:
for (int t = 0; t < 4; t++)
So now I am getting the results that I want without the student numbers, which is great!!
Thank you so much for all your help.
Now I just have to work on averaging each students score. (: