
Patrick B. answered 02/25/21
Math and computer tutor/teacher
using namespace std;
#include <iostream>
#include <time.h>
#include <stdlib.h>
int Go()
{
srand(time(0));
int num1 = rand() % 500 + 1;
int num2 = rand() % 500 + 1;
int iResponse;
int iCorrectAnswer;
int op=-1;
while ((op!=1) && (op!=2))
{
cout << "Please input 1 for Addition OR 2 for Subtraction :>";
cin >> op;
}
switch (op)
{
case 1:
{
iCorrectAnswer = num1 + num2;
cout << "What is: " << num1 << " + " << num2 << " = ??? ";
break;
} //case1
case 2:
{
//Do you want to GUARANTEE a positive answer by ensuring the num1 > num2 ???
// if so, uncommment the next line which swaps them if they are out of order....
//if (num1<num2) { int iTemp=num1; num1 = num2; num2=iTemp; }
iCorrectAnswer = num1 - num2;
cout << "What is: " << num1 << " - " << num2 << " = ??? ";
break;
}
} //switch
cin >> iResponse;
if (iCorrectAnswer ==iResponse)
{
cout << "Congratulations! You are correct!" << endl;
}
else
{
cout << "Sorry that is incorrect. The correct answer is " << iCorrectAnswer << endl;
}
} //Go
int main()
{
Go();
}