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?
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
usingnamespace 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");
}
}
}
}
elseif (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";
}
elseif (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.