Grading Program

I was practicing with a grading program, and i have a few questions. Here is the code

#include <iostream>
using namespace std;

int main()
{
double grade = 0;
char choise;
do
{
do
{
cout << "Grade (0-10):\n";
cin >> grade;
if (grade > 10 || grade < 0)
{
cout << "Invalid.\n";
}
} while (grade > 10 || grade < 0);
if (grade == 10)
cout << "A+ \n";
else
if (grade < 10 && grade >= 9)
cout << "A\n";
else
if (grade < 9 && grade >= 8)
cout << "B\n";
else
if (grade < 8 && grade >= 7)
cout << "C\n";
else
{
cout << "Fail :( \n";
}
cout << "Again? (Y/N): ";
cin >> choise;

} while (choise == 'Y' || choise == 'y');
system("pause");
}


If you writte a character where instead of a number in "grade", it jumps till the end. Why?

And i would like to add to the Y/N part another thing, if the user types something but Y / N then print an error message and ask again.

Thanks !
When inputting a number, don't input a single alphabet character, which makes the number invalid. If you choose to face a countless number of loops and try to deal with it yourself, there is a way. Try googling for more details.

1
2
3
4
5
6
7
8
9
10
11
for( ;; )
{
      cout << "Again? (Y/N) : ";
     cin >> choise;
     if(choise == 'y' || choise == 'Y') break;

     if(choise == 'n' || choise == 'N') break;
  
     cout << endl;
     cout << "Wrong input. Please enter again" << endl << endl;
}
If you write a character where instead of a number in "grade", it jumps till the end. Why?

as it is converted into its ASCII number (type casting)

to avoid that use cin.fail() function

And i would like to add to the Y/N part another thing, if the user types something but Y / N then print an error message and ask again.


simple... use another if-else statement
Last edited on
Topic archived. No new replies allowed.