Trouble inputting loops into file then reading the file

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);
Last edited on
Is "File successfully open"?

If so you might look at the wrong "test.txt". Take a look at the time stamp of the file. Does it change?
I just now started working on this again. I can get it to write to the txt file, however it only outputs the last user id and quizzes I enter. How can I get it to keep all three if I were to enter three students and their respective quiz scores? I am pretty sure I need to use a while loop outside of my for loop but I do not know what to even put in that loop.. Ideas?


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
int main()
{
	ifstream inFile;
	ofstream outFile;

	

	double student1avg, student2avg, student3avg, classAvg;

	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;


	while (studentID)
	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;
	}

	outFile.open("quizgrades.txt");
	if (outFile.is_open())
	{
		outFile << "File successfully open";
		outFile << studentID << " " << quiz1 << " " << quiz2 << " " << quiz3 << " " << quiz4;
		outFile.close();
	}
	else
	{
		cout << "Error opening file";
	}
it only outputs the last user id and quizzes I enter.

Since you do not store or write to the file the ID and Quizzes' grades of one student before moving to the next all previously given data are lost.And that is why you only see the last entry written to the file.

How can I get it to keep all three if I were to enter three students and their respective quiz scores?

You can either write to the file the data before entering data for the next student, or store them into an array or something and write them all together at the end.

I am pretty sure I need to use a while loop outside of my for loop but I do not know what to even put in that loop

I think the purpose of the while loop is to control input termination.Something like

1
2
3
4
5
6
7
8
9
while(!done)
{
    for(int i = 0;i < 4;i++)
    {
       //get grades for this student's quizes
    }
    //check if user wants to continue
    //if no done = true
}
Move line 38-48 up and put it after line 27
Topic archived. No new replies allowed.