<< This thread is split off from a discussion that originated here: [thread]62976[/thread] >>
Here;s something you can work on. It may not be exactly what you need but its a basis at least. Also, why do you need to check if the second number of the division is zero, from the second you say it will accept values from 1-20?? Anyway, I put the check in the program as well if u need it for later on.. Hope it helps, byee
#include <iostream>
#include <ctime>
using namespace std;
int DoMenu();
void addition(int random1, int random2);
void subtraction(int random1, int random2);
void multiplication(int random1, int random2);
void division(int random1, int random2);
int main()
{
int random1, random2;
srand(time(0));
int choice;
do
{
random1 = 1+(rand()%20); // from 1-20
random2 = 1+(rand()%20); // from 1-20
choice = DoMenu();
switch (choice)
{
case 0: cout << endl; break;
case 1: addition(random1,random2); break;
case 2: subtraction(random1,random2); break;
case 3: multiplication(random1,random2); break;
case 4:
{
if (random2 == 0)
cout << "Error: Division by zero, cannot proceed\n";
else
division(random1,random2);
} break;
default : cout << "Error in input\n\n"; break;
}
} while (choice != 0);
return 0;
}
int DoMenu()
{
int MenuChoice;
cout << "(1)Addition\n";
cout << "(2)Subtraction\n";
cout << "(3)Multiplication\n";
cout << "(4)Division\n";
cout << "(0)Quit\n";
cout << "-----------------\n";
cout << "Choice: ";
cin >> MenuChoice;
return MenuChoice;
}
void addition(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " + " << random2 << " = ";
cin >> answer;
if (answer == (random1+random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
void subtraction(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " - " << random2 << " = ";
cin >> answer;
if (answer == (random1-random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
void multiplication(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " * " << random2 << " = ";
cin >> answer;
if (answer == (random1*random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}
void division(int random1, int random2)
{
int answer;
cout << "\n" << random1 << " / " << random2 << " = ";
cin >> answer;
if (answer == (random1/random2))
cout << "Congratulations\n\n";
else
cout << "Incorrect\n\n";
}