I'm having trouble with my program and the nested loops. I can't seem to get it to output to the text file correctly. It is only outputting the last quiz results and student ID that I enter.
I also cannot figure out how to exactly break my loop. Even if I enter in a 0 to stop, the loop continues, prompting for the Student ID.
By the way, the program is obviously unfinished according to the instructions. I haven't quite gotten past the output and breaking the loop part.
Here are my 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.
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
Here is my code:
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 74
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace 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 < 3; 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;
}
ofstream outputFile;
outputFile.open("QuizGrades2.txt");
if (outputFile.is_open())
{
outputFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4;
outputFile.close();
}
else
{
cout << "Error opening file";
}
cout << "\nEnter 0 for no more students to enter. Enter 1 for more students.";
cin >> go;
}
}
system("pause");
return 0;
}
|