### Calculation errors?

Hello! I am trying to make this quiz about circles to help my younger brother do better in his math, but there are some problems... For the normal difficulty(I have not finished coding the harder difficulty), whenever the radius is generated as 5 and I have to calculate the circumference, the answer is wrong. The formula is 2pr, which is 2*3.14*5 in this case. When I enter 31.4, it says it is incorrect. Help please?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125`` ``````#include #include #include #include using namespace std; int main () { system ("color 0d"); int diffinput, radius, cirareainput; double pi, guess, circumference, area; pi=3.14; cout <<"Type 1 for Normal difficulty or 2 for a harder difficulty.\n"; cin >>diffinput; if (diffinput == 1) { srand( (unsigned)time( NULL ) ); radius = rand() % 9+1; cout << "Type 1 to calculate the circumference or 2 to calculate the area of the circle.(Take pi as 3.14)\n"; cin >> cirareainput; if (cirareainput == 1) { cout << "The radius of the circle is " << radius << ". Please calculate the circumference of the circle.\n"; cin >> guess; circumference = 2 * pi * radius; if (guess == circumference) { cout << "Your answer is correct!\n"; } else { cout <<"Your answer is incorrect. Try again.\n"; cout << "The radius of the circle is " << radius << ". Please calculate the circumference of the circle.\n"; cin >> guess; circumference = 2*pi*radius; if (guess == circumference) { cout << "Your answer is correct!\n"; } else { cout <<"Your answer is incorrect. Try again."; cout << "The radius of the circle is " << radius << ". Please calculate the circumference of the circle.\n"; cin >> guess; circumference = 2*pi*radius; if (guess == circumference) { cout << "Your answer is correct!\n"; } else { cout <<"Your answer is incorrect. You have exceeded the maximum amount of tries.\n"; system("exit"); } } } } else if (cirareainput == 2) { cout << "The radius of the circle is "<> guess; area = pi*radius*radius; if (guess == area) { cout <<"Your answer is correct!"; } else { cout <<"Your answer is incorrect. Try again."; cout << "The radius of the circle is "<> guess; area = pi*radius*radius; if (guess == area) { cout <<"Your answer is correct!"; } else { cout <<"Your answer is incorrect. Try again."; cout << "The radius of the circle is "<> guess; area = pi*radius*radius; if (guess == area) { cout <<"Your answer is correct!"; } else { cout <<"Your answer is incorrect. You have exceeded the maximum amount of tries."; } } } } else cout << "Parameter is not defined.\n"; } else if (diffinput == 2) { srand( (unsigned)time( NULL ) ); radius = rand() % 199+1; cout << "Type 1 to calculate the circumference or 2 to calculate the area of the circle.(Take pi as 3.14)\n"; cin >> cirareainput; if (cirareainput == 1) { cout << "The radius of the circle is " << radius << ". Please calculate the circumference of the circle.\n"; cin >> guess; circumference = 2*pi*radius; if (guess == circumference) { cout << "Your answer is correct!\n"; } else { cout <<"Your answer is incorrect. You have exceeded the maximum amount of tries.\n"; system ("exit"); } } } system ("pause"); }``````
 When I enter 31.4, it says it is incorrect. Help please?

 ``1234`` ``````if (guess == circumference) { cout << "Your answer is correct!\n"; }``````

You have to be careful comparing doubles directly. They are stored as binary fractions and cannot represent all real numbers exactly. For this reason they should not be used in direct comparisons. Consider this:

 ``123456`` ``````float a = 0.1; // a == 0.09999997 float b = 10 * a; // b== 0.9999997 if (b == 1.0 ) { //false } ``````

Changing the type to double doesn't help.

You can have a variable which is an arbitrary precision. Then calculate whether the absolute value of answer minus guess is less than the precision. If so then it is "equal" in terms of the precision.

 ``1234567891011121314`` ``````#include double Guess; double Answer; double MyPrecision = 0.001; //set the values for Guess and Answer if (abs(Answer - Guess) < MyPrecision) { cout << "Answer is correct" << endl; } else { cout << "Answer is wrong" << endl; }``````

Other things I notice:

Your variable pi should be const.

The program structure would be better if you used switch statements to organise the menu options.

HTH

Last edited on
Thank you! I just started coding and this is my second project. Didn't notice that the values were not exact. :)
A type `double` can hold about 15 decimal digits. It doesn't make sense to use this `pi=3.14`, it means most of the digits will be incorrect.
http://www.cplusplus.com/forum/beginner/83485/#msg448028
Topic archived. No new replies allowed.