Writing a Simple Math tutoring program: The program will ask the user if they wa
ID: 3530248 • Letter: W
Question
Writing a Simple Math tutoring program: The program will ask the user if they want to practice addition or subtraction. If their choice was valid, the program randomly chooses two numbers between 0 and 100 and stores them in variables num1 and num2. The problem is then written out to the user as either num1 OR num1 +num2 -num2 ----- ----- depending on their choice. The program should calculate the correct answer and ask the user to provide their answer as input. The two values, the computer-calculated one, and the user-provided one should be compared. If the user provided the correct answer, they should be congratulated. If their answer is wrong, a message should be printed showing the correct answer. The program should: Write out the choices, addition or subtraction, to the user and ask them for their choice. Use an if/else if decision structure to determine which arithmetic operation the user chose. If their choice does not match either addition of subtraction, write out an error message before exiting the program. For each choice, Generate the two numbers using rand(). Limit the values to between 0 and 100. Then display the arithmetic problem using standard output. Calculate the correct answer and store the result. Ask the user for their answer and read in the value Compare the user's answer to the computed value Congratulate the user if they are correct. If their answer was wrong, write out the correct answer.Explanation / Answer
Formated exactly how you asked for, Enjoy!
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int choice, num1, num2, guess, answer;
cout << "1. Add" << endl
<< "2. Subtract" << endl
<< "Enter Your Choice (1 or 2): ";
//generate random numbers mod101 gives numbers from 0 to 100
num1 = rand()1;
num2 = rand()1;
cin >> choice;
if(choice < 1 || choice > 2){
cout << "Invalid choice. Exiting program..." << endl;
exit(-1);
}
//addition
else if( choice == 1){
answer = num1 + num2;
//display equation
cout << endl << " ";
//add spaces according to how large number is
if(num1 < 10)
cout << " " << num1 << endl;
else if(num1 < 100)
cout << " " << num1 << endl;
cout << "+ ";
if(num2 < 10)
cout << " " << num2 << endl;
else if(num2 < 100)
cout << " " << num2 << endl;
cout << "------" << endl;
}
//subtraction
else{
answer = num1 - num2;
//display equation
cout << " ";
//add spaces according to how large number is
if(num1 < 10)
cout << " " << num1 << endl;
else if(num1 < 100)
cout << " " << num1 << endl;
cout << "- ";
if(num2 < 10)
cout << " " << num2 << endl;
else if(num2 < 100)
cout << " " << num2 << endl;
cout << "------" << endl;
}
cout << "Enter your answer: ";
cin >> guess;
if(guess == answer)
cout << "Congratulations that is the correct answer!" << endl;
else
cout << "Sorry, that is incorrect. The correct answer is: " << answer << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.