do-while statement

I am new to c++ and am in my first class..we are currently working on the do-while statement and i am stuck. Here is the exercise:

Write, compile, and run a C++ program that continuously requests a grade to be entered. If the grade is less than 0 or greater than 100, your program should print a message informing the user that an invalid grade has been entered; else, the grade should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute and display the average of the valid grades entered.This is what i've got so far....





#include <iostream>
using namespace std;


int main ()
{

int grade;

do {
cout << "Enter a grade : ";
cin >> grade;
} while (grade > 0 || grade <100);
else
cout >> "The grade just entered is invalid";



system("pause");
return 0;
}



I am confused about the part of the program that says:

"else, the grade should be added to a total. When a grade of 999 is entered, the program should exit the repetition loop and compute and display the average of the valid grades entered."


Help Please and Thank You
while (grade > 0 || grade <100); with this the way it is, even a negative number would work. use && (and) instead of || (or)

create a variable to store the total of the valid grades

"else" can only be used with if statements
Last edited on
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
#include <iostream>
using namespace std;

int main()
{
	//Declare Variables
	int grade = 0;
	double total = 0.0;
	int numberOfGrades = 0;
	double average = 0.0;
		
		do  //begin loop
		{
			cout << "Enter a grade. (Enter 999 to quit.):  ";
			cin >> grade;

			if (grade < 0 || grade > 100)
				cout << "The grade you entered is invalid" << endl;
			else
			{
				numberOfGrades += 1;
				total = total + grade;
			} //end if
		} while (grade != 999); //end loop

		average = total / numberOfGrades;
		cout << endl << endl;
		cout << "The average of the grades entered is " << average << endl;

		system("pause");
		return 0;
} //end of main function 
This compiles and works correctly, but it would be better if line 18 didn't print out after entering the sentinel value.

999 is a sentinel or flag value that will exits the loop after it's entered.

Hope this helps. I'm new at C++ too.
Topic archived. No new replies allowed.