Hello, I am having trouble with a problem. I have been messing around with it for a few hours but still haven't even been able to make anything output to the text file. Here is my assignment, but I can not even get past this part to read from the file I am trying to make! Also forget about the nested loops in the directions, because I was having too much trouble trying to figure that out.
***NOTHING is showing up into my test.txt file after compiling?!
In this assignment, you will write a program in C++ 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 student’s quizzes to a file. Then read the data from the file in order to display the student’s average score and the class average.
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.
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 berore the while loop, add each student’s total into the classTotal following the for loop, then calculate the classAverage after the while 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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string test;
int studentID, quiz1, quiz2, quiz3, quiz4, quiz_total, choice;
cout << "Enter student ID number, Quiz 1 Grade, Quiz 2 Grade , Quiz 3 Grade, Quiz 4 Grade" << endl;
for (int x = 0; x < 3; x++){
cout << "Enter student ID: ";
cin >> studentID;
cout << "Enter quiz grade 1: ";
cin >> quiz1;
cout << "Enter quiz grade 2: ";
cin >> quiz2;
cout << "Enter quiz grade 3: ";
cin >> quiz3;
cout << "Enter quiz grade 4: ";
cin >> quiz4;
cout << endl;
cout << "Enter 0 for no more students. Enter 1 for more students." << endl;
cin >> choice;
if (choice == 0)
break;
if (choice == 1)
continue;
}
//declaring file and opening it
ofstream outFile("test.txt");
outFile.open("test.txt");
if (outFile.is_open())
{
outFile << "File successfully open";
getline(cin, test);
outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4;
outFile.close();
}
else
{
cout << "Error opening file";
}
return(0);
|