-3

I wrote a program that outputs the name equivalent of a numerical grade the user wrote into the console.

1 being FAIL

2 being SATISFACTORY

3 being GOOD

4 being VERY GOOD

5 being EXCELLENT

Now I would like to ask the user does he want to continue inputting the grades AFTER the first one runs,

e.g

input grade -> 5 Excellent Would you like to continue inputting grades? 1 - yes/ 0 -no

I'm clueless where to put the do while bool is true..

int main()
{
    int grade;


        do
        {
            cout << "input your grade: \n";
            cin >> grade;

            if (grade < 1 || grade > 5)
            {
                cout << "input grade again!\n";
            }



        } while (grade < 1 || grade > 5);



        switch (grade)
        {
        case 1:
            cout << "fail\n";
            break;

        case 2:
            cout << "satisfactory\n";
            break;

        case 3:
            cout << "good\n";
            break;

        case 4:
            cout << "very good\n";
            break;

        case 5:
            cout << "excellent\n";
            break;



        }

    return 0;
}
Bahman Parsa Manesh
  • 2,314
  • 3
  • 16
  • 32
Doppel
  • 1
  • 3

1 Answers1

1

You just need another do-while statement to have a repeated process. I've initialized a char variable named userChoice (always make the initialization to avoid undefined behaviors and hard to track errors) to check in every iteration what user wants (to continue or to end the program).

//previous codes...
char userChoice = 'n';
do{
    // previous codes which you want to be repeated
    std::cout << "\nDo you want to continue ? (y = Yes, n = No ) "; // ask the user to continue or to end
    std::cin >> userChoice; // store user's input in the userChoice variable

}while(userChoice); // if user's input is 'y' then continue with next iteration
//rest of the code...

Also, Why is "using namespace std" considered bad practice?

Ozan Yurtsever
  • 1,274
  • 8
  • 32