How to fix the division case. As you notice when comparing two floating point nu
ID: 3852350 • Letter: H
Question
How to fix the division case. As you notice when comparing two floating point numbers, it gives the “wrong” result as the answer always rounds up to integers.
(HELP Me with fixing my code, only the divsion part, the rest is fine, thanks)
using namespace std;
int main()
{
int choice = 0,num1, num2, ans, user_ans;
while (choice != 5)
{
cout << " Math Menu ";
cout << "------------------------ ";
cout << "1: addition ";
cout << "2: subtraction ";
cout << "3: multiplication ";
cout << "4: division ";
cout << "5: Exit ";
cout << "Enter your choice (1-5):";
cin >> choice ;
while ((choice > 5) or ( choice < 1))
{
cout << "Invalid Entry! ";
cout << "Enter your choice again:";
cin >> choice;
}
if (choice == 5)
cout << "Thank you for using, Press enter to exit. ";
else if ( choice == 1)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "+ " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 + num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
else if ( choice == 2)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "- " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 - num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
else if ( choice == 3)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "x " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 * num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
else if (choice == 4)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "/ " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 / num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
}
return 0;
}
Math Tutor Menu 1: addition 2: subtraction 3: multiplication 4: division 5: quit Enter your choice (1~5):4 Enter the answer 274 23 0.00 Your answer is wrong, the correct answer is: 1 Process returned 0 (0x0 execution time 2.274 s Press ENTER to continueExplanation / Answer
using namespace std;
int main()
{
int choice = 0,num1, num2, ans, user_ans;
while (choice != 5)
{
cout << " Math Menu ";
cout << "------------------------ ";
cout << "1: addition ";
cout << "2: subtraction ";
cout << "3: multiplication ";
cout << "4: division ";
cout << "5: Exit ";
cout << "Enter your choice (1-5):";
cin >> choice ;
while ((choice > 5) or ( choice < 1))
{
cout << "Invalid Entry! ";
cout << "Enter your choice again:";
cin >> choice;
}
if (choice == 5)
cout << "Thank you for using, Press enter to exit. ";
else if ( choice == 1)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "+ " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 + num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
else if ( choice == 2)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "- " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 - num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
else if ( choice == 3)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "x " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 * num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
else if (choice == 4)
{
srand(time(NULL));
num1 = rand() %999, num2 = rand() % 999;
cout << "Please Enter the answer ";
cout << " " << num1 <<" ";
cout << "/ " << num2 <<" ";
cout << "------- ";
cin >> user_ans ;
ans = num1 / num2;
if ( user_ans == ans)
cout << "congratulations you got the correct answer!";
if ( user_ans != ans)
cout << "The answer is in correct, the correct answer is: " << ans << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.