Hi guys, my assignment is to create a program that sets up a menu for a user to choose from. The menu should consist of an addition, subtraction, multiplication and division problem. It seems to work well, but the only thing that's not working is the division problem. Whenever I try to test it out, I don't get the correct answer to the random division problem that I set up. What is wrong here? I think it has to do with floats and division but I can't pinpoint the error.
/ This program displays a menu allowing the user to choose from a set of 4 math operations and a choice to exit the program. Once the user selects one of the 4 math operations, they can enter the answer. If the answer is correct, the user will be congratulated. If the answer is wrong, the correct answer will appear. The menu will appear again allowing the user to choose from an option.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
int main()
{
// Constants for the menu choices
const int SEED = 999;
const int ADDITION_CHOICE = 1,
SUBTRACTION_CHOICE = 2,
MULTIPLICATION_CHOICE = 3,
DIVISION_CHOICE = 4,
QUIT_CHOICE = 5;
// Variables
int userchoice;
float studentanswer;
float correctanswer;
do
{ // Beggining of do-while loop
unsigned int number1 =0;
unsigned int number2 =0;
srand(unsigned(time(0)));
number1 = 1+rand() % SEED;
number2 = 100+rand() % SEED;
// Display the menu
cout << "Choose from the following options:\n\n"
<< "1. Addition\n"
<< "2. Subtraction\n"
<< "3. Multiplication\n"
<< "4. Division\n"
<< "5. Quit\n"
<< "Enter your choice: \n";
cin >> userchoice;
// Validates the input
if (userchoice < ADDITION_CHOICE || userchoice > QUIT_CHOICE)
{
cout << "Please choose a valid number from 1 to 5\n\n"
<< "1. Addition\n"
<< "2. Subtraction\n"
<< "3. Multiplication\n"
<< "4. Division\n"
<< "5. Quit\n";
cin >> userchoice;
}
// Respond to user's choice
switch (userchoice)
{ // Addition
case ADDITION_CHOICE:
cout << "Solve the problem below: \n";
cout << number1 << " + " << number2 << " = " << endl;
correctanswer = number1 + number2;
cin >> studentanswer;
if (studentanswer == correctanswer)
cout << "Correct! Congratulations.\n";
else
cout << "That is incorrect. The correct answer is " << correctanswer << endl;
break;
// Subtraction
case SUBTRACTION_CHOICE:
cout << "Solve the problem below: \n";
cout << number1 << " - " << number2 << " = " << endl;
correctanswer = number1 - number2;
cin >> studentanswer;
if (studentanswer == correctanswer)
cout << "Correct! Congratulations.\n";
else
cout << "That is incorrect. The correct answer is " << correctanswer << endl;
break;
// Multiplication
case MULTIPLICATION_CHOICE:
cout << number1 << " * " << number2 << " = " << endl;
correctanswer = number1 * number2;
cin >> studentanswer;
if (studentanswer == correctanswer)
cout << "Correct! Congratulations.\n";
else
cout << "That is incorrect. The correct answer is " << correctanswer << endl;
break;
// Division
case DIVISION_CHOICE:
cout << number1 << " / " << number2 << " = " << endl;
correctanswer = number1 / number2;
cin >> studentanswer;
if (studentanswer == correctanswer)
cout << "Correct! Congratulations.\n";
else
cout << "That is incorrect. The correct answer is " << correctanswer << endl;
break;
// Exit
case QUIT_CHOICE:
exit(0);
break;
}
}while(userchoice != 5);
return 0;
}