Sentinel values and repition problem (HOMEWORK HELP!)

Hi guys, first post go easy on me :D.

Question:

write, compile run a program that continously requests a grade to be entered. If the grad is less than 0 or more than 100 you program should print a message Invalid data . else the data should be added to the total. When a grade of 999 is entered, the program should exit the repition loop and compute and display the average.


Here is what i worked out so far...
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
#include <iostream>
using namespace std;

int main()
{
double average,total,grade;
int count = 0;

cout << "Please enter a grade: ";
      cin >> grade;
      
while (grade!=999)
      
      while (grade > 0 && grade <=100)
      { 
            cout << "Please enter a grade: ";
            cin >> grade;
            total = total + grade;
            count++;
      }   
      
      cout << "SORRY INVALID TRY AGAIN:";
      else
      average = total/count;
      cout << "Your total grade average is << average << endl;
        
system("pause");
return 0;
} 


This obviously doesn't work and im stuck at this question for an hour?? anyone have a clue how to fix this? thanks.
1) You have an open quotes in line 25 without a close quotes.
2) You have a while loop without a "{" on line 12
3) You have an "else" on line 23 without an "if" anywhere in your program

I suggest you read http://www.cplusplus.com/doc/tutorial/control/ to learn these structures.

Let me know if you're still stuck after that.
I'm still confused,

i believe i have to use a nested loop OR an if else with a while loop.

I can do the while loop perfectly while (grade > 0 && grade <=100)......{statement} cout<<"sorry invalid".

But once i have to apply this variable grade = 999, i am stuck.
Could you post your latest revision of your code? If I see what you've changed, I can help you iterate.
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
#include <iostream>
using namespace std;

int main()
{
double average,total,grade;
int count = 0;

cout << "Please enter a grade: ";
      cin >> grade;
while (grade!=999)
      { 
      cout << "Please enter a grade: ";
      cin >> grade;
      }
            
      while (grade>0 && grade<=100)
            {
            total = total + grade;
            count++;
            }
            cout << "SORRY INVALID CODE:";
      
      average = total/count;
      cout << "\nYour total grade average is" << average << endl;
      
system("pause");
return 0;
}
You are now using valid code. Your current problem is a logical one. I have a question for you that I think will help you learn.

What could you input to this program to reach line 20: count++;? What exactly do you type in at the cin >> prompts?
cout << "Please enter a grade: ";
cin >> grade;
total = total + grade;
count++;

when i type any number 101 still asks for number doesn't say invalid code. when i type 999 it jumps to the end but also adds 999 to the average and i get a wierd error :/
Topic archived. No new replies allowed.