Help with Nested Loops

Mar 22, 2016 at 3:12am
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;


}
Last edited on Mar 22, 2016 at 11:24am
Mar 22, 2016 at 5:31am
first for exiting the loop

cout << "\nEnter 0 for no more students to enter. Enter 1 for more students.";
cin >> go; should be outside the for loop
else
{
cout << "Error opening file";
}

} //end for
cout << "\nEnter 0 for no more students to enter. Enter 1 for more students.";
cin >> go;
} //end while


and open the file in append mode

outputFile.open("/home/pss1kor/TestC/Test/src/QuizGrades2.txt",std::fstream::in | std::fstream::out | std::fstream::app);
Mar 22, 2016 at 9:40pm
Thank you so much for the help! I also changed the amount of iterations to 1 so it asks to add a new student every time.
Now all I need to do is figure out how to create a nested loop (A while loop with a for loop inside) to read all the test scores separate, and average them separately...
Mar 23, 2016 at 9:22am
need not to create the new loop inside that loop only u can do it
after entering the Quiz4 u can calculate the average and copy in the same file
Topic archived. No new replies allowed.