Help with using two while loops

My program won't even run without debugging. What am I doing wrong here?

Write a program that uses two while loops. The outer loop asks the user if they want to enter the grades for a new class. If yes, they should be asked for the class name and then asked another question to ask them if they want to enter grades. If y or Y, the program should accept as many student grades that were given in a specific class. In the inner loop you should total up all the grades and count them as well.

At the end of the inner loop, display the class name, the total, the average and how many items were entered for that class.

If the average is greater than 80, display a message that this group of students did very well in this class.

if the average is greater than 50 but less or equal to 80 display a message that the class might need to be retooled.

In the outer while loop, again the user should be asked if they want to enter the grades for a new class. If Y or y, initialize the total to zero to start over again for the new class.
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//declare variables
	string className;
	int totalGrade = 0;
	int avgGrade = 0;
	char c = ' ';
	char c2 = ' ';
	int grd = 0;

	while (1)
	{
		cout << "Do you want to enter the grades for a new class?: ";
		cin >> c;

		if (c == 'y' || c == 'Y')
		{
			cout << "Enter the class name: ";
			cin >> className;

			while (1)
			{
				cout << "Do you want to enter a grade?: ";
				cin >> c2;

				if (c2 == 'y' || c2 == 'Y')
				{
					cout << "Enter the grade value: ";
					cin >> grd;

					totalGrade = totalGrade + grd;
					count++;
				}
				else
				{
					break;
				}
			}

			avgGrade = totalGrade / ((int)count);

			cout << "\nClass Name: " << className;
			cout << "\nTotal Grades: " << totalGrade;
			cout << "\nAverage Grades: " << avgGrade;
			cout << "\nNumber of grades entered: " << count;

			if (avgGrade>80)
			{
				cout << "\nThe group of students did very well in class." << endl << endl;
			}
			if (avgGrade>50 && avgGrade <= 80)
			{
				cout << "\nThe class might need to be retooled." << endl << endl;
			}

		}
	}
	system("pause");
	return 0;
}
It does not look like you have declared count
The count variable you are using has not been declared. You should declare it and probably initialize to zero.
...and oh the outer loop is never going to end. You should create a control for it.

Aceix.
Last edited on
Since className, totalGrade, avgGrade and count all need to reset for each class, you should move the definitions of these inside the outer loop (right after line 21).

Change avgGrade to a double since it can be a real number. When computing it, you'll have to ensure that the division is done using floating point arithmetic instead of integer arithmetic. To do this, just cast one of the values to a double:
avgGrade = static_cast<double>(totalGrade) / count;
Topic archived. No new replies allowed.