#include <iostream>
#include <string>
#include <Windows.h>
void Problem2();
int main()
{
Problem2();
return 0;
}
void Problem2()
{
usingnamespace std;
double score;
char grade, flag = 'n';
do {
if (flag == 'y') {
cout << "Invalid score. Score must be between 0 and 100" <<"\n";
Sleep(1500);
}
cout << "Enter a score: ";
cin >> score;
flag = 'y';
} while (score < 0 || score >100);
if (90 <= score && score <= 100)
grade = 'A';
elseif (80 <= score && score < 90)
grade = 'B';
elseif (70 <= score && score < 80)
grade = 'C';
else
grade = 'F';
cout << "Your grade is " << grade << "\n";
}
this code checks the score and categorize them into grades;
According to the code, the score of x=89.99999999999999999999
(20 9's after decimal dot)
must award grade of B, but when I run it it actually awards A. What I suspect is that x is rounded to nearest number 90, but why does this happen? As far as I'm aware, doesn't double data type can store data up to far better accuracy?