Jul 1, 2016 at 6:20am UTC
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 !
Jul 1, 2016 at 8:12am UTC
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 Jul 1, 2016 at 8:13am UTC