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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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 "<<radius<< ". Please calculate the area of the circle.\n";
	cin >> 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 "<<radius<< ". Please calculate the area of the circle.\n";
	cin >> 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 "<<radius<< ". Please calculate the area of the circle.\n";
	cin >> 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?


1
2
3
4
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:

1
2
3
4
5
6
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cmath>

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.