Need help with a do-while loop!

Solved.
Last edited on
To stop a While/Do While/Switch loop, you can use break, like this:
1
2
3
4
5
6
7
8
9
10
11
12
   do
      {
      cout << "Please enter a grade: ";
      cin >> grade;
      if (grade < 0 || grade > 100)
      {}
      else
      {
            cout << "Grade: " << grade << endl;
            break;
      }
}  while (1);
Last edited on
Im sorry, I should have said this. I am not allowed to use breaks :/
Last edited on
I bet it's for an assignment then.
You then can do something like this:
1
2
3
4
5
6
do
      {
      cout << "Please enter a grade: " << endl;
      cin >> grade;
}  while (grade < 0 || grade > 100);
cout << "Grade: " << grade << endl;

So while grade is invalid, it will keep on asking for an input, and once it is valid, it will stop looping and display the grade.
This question is:
Using a do-while statement, write, compile and run a C++ program to accept a grade. The program should request a grade continuously as long as an invalid grade is entered. An invalid grade is any grade less than 0 or greater than 100. After a valid grade has been entered, your program should display the value of the grade entered.

My Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
   int grade;
   
   do
{
      cout << "Please enter a grade: ";
      cin >> grade;
      if (grade < 0 || grade > 100)
{
}
       else
         cout << "Grade: " << grade << endl;
}  while (1);
   cin.ignore();
   return 0;
}



How do I get the program to stop after a valid grade is entered?
Topic archived. No new replies allowed.