Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program displays a menu allowing the user to select an addition, subtractio

ID: 3646047 • Letter: T

Question

This program displays a menu allowing the user to select an addition, subtraction, multiplication or division problem. The numbers should be generated randomly and after the problem is answered it should show the menu again until the user exits. I have wrote this program three times and I can not get the fivision to work properly. The answer is right and it matches up with the computers answer but C++ says it is wrong for some reason. This is my code, please see why the division won't work right. It is like after I enter my division answer C++ goes into a non stop infanant loop. Here is my code.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{


srand(time(0));
const int SEED = 999;
int num1;
int num2;
int result;
int answer;

num1 = 1+rand() % SEED;
num2 = 1+rand() % SEED;
int choice;

do
{
cout << setprecision(2) << fixed << endl;
cout << " MENU " << endl;
cout << " 1. Addition ";
cout << " 2. Subtraction ";
cout << " 3. Multiplication ";
cout << " 4. Division ";
cout << " 5. Quit the program ";
cout << "Welcome to the Math Tutor Boys and Girls!!!" << endl;

cout<< "Please make your selection" << endl;

cin >> choice;

switch (choice)
{
case 1: cout << num1 << " + " << num2 << " = " << endl;
result = num1 + num2;
break;

case 2: cout << num1<< " - " << num2 << " = " << endl;
result = num1 - num2;
break;

case 3: cout << num1 << " * " << num2 << " = " << endl;
result = num1 * num2;
break;

case 4: cout << setw(5) << num1 << " / " << setw(3) << num2 << " = " << endl;
result = num1 / num2;
break;

case 5: exit(0);
break;

}

cout << "What is your answer?" << endl;
cin >> answer;

if (answer == result)
cout << "Great Job!!!!!!!!!!!!!!" << endl;

else
cout << "I'm sorry that answer is wrong, the correct answer is " << result << endl;

}while(choice < 5);
system("pause");



return 0;
}

Explanation / Answer

Yeah,that was a problem and i solved it. Include the num1,num2 random initialization inside loop. I have pasted some part of mu code to indicate its position: Code: srand(time(NULL)); const int SEED = 999; int num1; int num2; float result; float answer; int choice; do { cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote