Write a program to be used as a math tutor for a young student. The program shou
ID: 3622702 • Letter: W
Question
Write a program to be used as a math tutor for a young student. The program should display a menu allowing the user to select addition, subtraction, multiplication or division. The final selection on the menu should allow the user to quit and exit the program. Each of modules, Addition, Subtraction, Multiplication and Division should be separate functions called from Main. 1. Display the menu-Accept user input on the operation to be performed 2. Generate 2 random numbers from 1-20 - Pass the random numbers to each function as arguments. 3. If user selects division, ensure the divisor is not zero. 4. Display the numbers and accept the users answers from the keyboard on whether they perform addition, subtraction, multiplication or division 5. Compute the correct answer with your program. Compare the users answer to the answer you calculated in your program. 6. If the user enters the correct answer, display the message Congratulations..You are Correct 7. If the user enters the incorrect answer, display the message Incorrect..The correct answer is... display the correct answer. 8. The program should display the menu again, until the user ends the program by choosing to quit ------------------------------------------------------------------------------------------------------------------------------- ok so i got most of this figured out......i cant get the random numbers to work within each of the function such as addition..the code will be the same for the output and computations......everything else is almost complete but that is the only thing that is really annyoying me........ here is the full unfinished code // Author: Me // BCS 120 // Description: C++ Math Tutor #include <iostream> //Header Declaration #include <iomanip> using namespace std; int ans1, num1, num2; // declare num1 and num2 int choice; // variable choice void mainmenu(); // Main menu void addition(); // addition function void subtraction(); // subtraction function void multiplication(); // multiplication function void division(); // division function bool start = true; // start int main() { int random1 = rand()% 20+1; // Random number will start at 0 so add one cout << "First random number is: " << random1 << endl; int random2 = rand()% 20+1; // Random number will start at 0 so add one cout << "Second random number is: " << random2 << endl; if (start = true) mainmenu (); else if (start = false); system("pause"); return 0; } void addition() // perform addition. { cout << "What is" << random1 << " + " << random2 << " = ?" << endl; cout << endl; cin >> ans1; cout << "You answered that" << random1 << " + " << random2 << "is equal to: " << ans1 << endl; if(ans1==(random1+random2)) { cout << "Congratulations..You are Correct" << endl; } else cout << "Incorrect..The correct answer is..." << random1 << " + " << random2 << " = " << (random1+random2) << endl; mainmenu(); } void subtraction() //do subraction function. { cout << endl; cout << "Enter a number to subtract from: " << endl; cin >> num1 ; cout << "Enter a number to subract with: " << endl; cin >> num2 ; cout << endl; cout << num1 << " - " << num2 <<" = "<< (num1-num2) << endl; cout << endl; mainmenu(); } void multiplication() //do multiplication function. { cout << endl; cout << "Enter a number to multiply: " << endl; cin >> num1 ; cout << "Enter a second number to multiply by: " << endl; cin >> num2 ; cout << endl; cout << num1 << " X " << num2 <<" = "<< (num1*num2) << endl; cout << endl; mainmenu(); } void division() // do division function. { cout << endl; cout << "Enter a number to divide: " << endl; cin >> num1 ; cout << "Enter a number to divide by: " << endl; cin >> num2 ; cout << endl; cout << num1 << " / " << num2 << " = " << (num1/num2) << endl; cout << endl; mainmenu(); } void mainmenu() { cout << endl; cout << "*** Main Menu ***" << endl; cout << endl; cout << "Please choose from the following, press [5] to quit: " << endl; //main option cout << endl; /*main menu options */ cout << " Press [1] for Addition" << endl; cout << " Press [2] for Subtraction" << endl; cout << " Press [3] for Multiplication" << endl; cout << " Press [4] for Division" << endl; cout << endl; cin >> choice; //ask user to enter selection, places it in int choice switch (choice) { case 1: addition(); break; case 2: subtraction(); break; case 3: multiplication(); break; case 4: division(); break; case 5: (start = false); int main(); cout<<"Exiting..."<<endl<<endl; break; default: cout << " INVALID ENTER, Try again..." << endl << endl; cin.clear(); cin.sync(); mainmenu(); } } Write a program to be used as a math tutor for a young student. The program should display a menu allowing the user to select addition, subtraction, multiplication or division. The final selection on the menu should allow the user to quit and exit the program. Each of modules, Addition, Subtraction, Multiplication and Division should be separate functions called from Main. 1. Display the menu-Accept user input on the operation to be performed 2. Generate 2 random numbers from 1-20 - Pass the random numbers to each function as arguments. 3. If user selects division, ensure the divisor is not zero. 4. Display the numbers and accept the users answers from the keyboard on whether they perform addition, subtraction, multiplication or division 5. Compute the correct answer with your program. Compare the users answer to the answer you calculated in your program. 6. If the user enters the correct answer, display the message Congratulations..You are Correct 7. If the user enters the incorrect answer, display the message Incorrect..The correct answer is... display the correct answer. 8. The program should display the menu again, until the user ends the program by choosing to quit ------------------------------------------------------------------------------------------------------------------------------- ok so i got most of this figured out......i cant get the random numbers to work within each of the function such as addition..the code will be the same for the output and computations......everything else is almost complete but that is the only thing that is really annyoying me........ here is the full unfinished code // Author: Me // BCS 120 // Description: C++ Math Tutor #include <iostream> //Header Declaration #include <iomanip> using namespace std; int ans1, num1, num2; // declare num1 and num2 int choice; // variable choice void mainmenu(); // Main menu void addition(); // addition function void subtraction(); // subtraction function void multiplication(); // multiplication function void division(); // division function bool start = true; // start int main() { int random1 = rand()% 20+1; // Random number will start at 0 so add one cout << "First random number is: " << random1 << endl; int random2 = rand()% 20+1; // Random number will start at 0 so add one cout << "Second random number is: " << random2 << endl; if (start = true) mainmenu (); else if (start = false); system("pause"); return 0; } void addition() // perform addition. { cout << "What is" << random1 << " + " << random2 << " = ?" << endl; cout << endl; cin >> ans1; cout << "You answered that" << random1 << " + " << random2 << "is equal to: " << ans1 << endl; if(ans1==(random1+random2)) { cout << "Congratulations..You are Correct" << endl; } else cout << "Incorrect..The correct answer is..." << random1 << " + " << random2 << " = " << (random1+random2) << endl; mainmenu(); } void subtraction() //do subraction function. { cout << endl; cout << "Enter a number to subtract from: " << endl; cin >> num1 ; cout << "Enter a number to subract with: " << endl; cin >> num2 ; cout << endl; cout << num1 << " - " << num2 <<" = "<< (num1-num2) << endl; cout << endl; mainmenu(); } void multiplication() //do multiplication function. { cout << endl; cout << "Enter a number to multiply: " << endl; cin >> num1 ; cout << "Enter a second number to multiply by: " << endl; cin >> num2 ; cout << endl; cout << num1 << " X " << num2 <<" = "<< (num1*num2) << endl; cout << endl; mainmenu(); } void division() // do division function. { cout << endl; cout << "Enter a number to divide: " << endl; cin >> num1 ; cout << "Enter a number to divide by: " << endl; cin >> num2 ; cout << endl; cout << num1 << " / " << num2 << " = " << (num1/num2) << endl; cout << endl; mainmenu(); } void mainmenu() { cout << endl; cout << "*** Main Menu ***" << endl; cout << endl; cout << "Please choose from the following, press [5] to quit: " << endl; //main option cout << endl; /*main menu options */ cout << " Press [1] for Addition" << endl; cout << " Press [2] for Subtraction" << endl; cout << " Press [3] for Multiplication" << endl; cout << " Press [4] for Division" << endl; cout << endl; cin >> choice; //ask user to enter selection, places it in int choice switch (choice) { case 1: addition(); break; case 2: subtraction(); break; case 3: multiplication(); break; case 4: division(); break; case 5: (start = false); int main(); cout<<"Exiting..."<<endl<<endl; break; default: cout << " INVALID ENTER, Try again..." << endl << endl; cin.clear(); cin.sync(); mainmenu(); } } // Author: Me // BCS 120 // Description: C++ Math Tutor #include <iostream> //Header Declaration #include <iomanip> using namespace std; int ans1, num1, num2; // declare num1 and num2 int choice; // variable choice void mainmenu(); // Main menu void addition(); // addition function void subtraction(); // subtraction function void multiplication(); // multiplication function void division(); // division function bool start = true; // start int main() { int random1 = rand()% 20+1; // Random number will start at 0 so add one cout << "First random number is: " << random1 << endl; int random2 = rand()% 20+1; // Random number will start at 0 so add one cout << "Second random number is: " << random2 << endl; if (start = true) mainmenu (); else if (start = false); system("pause"); return 0; } void addition() // perform addition. { cout << "What is" << random1 << " + " << random2 << " = ?" << endl; cout << endl; cin >> ans1; cout << "You answered that" << random1 << " + " << random2 << "is equal to: " << ans1 << endl; if(ans1==(random1+random2)) { cout << "Congratulations..You are Correct" << endl; } else cout << "Incorrect..The correct answer is..." << random1 << " + " << random2 << " = " << (random1+random2) << endl; mainmenu(); } void subtraction() //do subraction function. { cout << endl; cout << "Enter a number to subtract from: " << endl; cin >> num1 ; cout << "Enter a number to subract with: " << endl; cin >> num2 ; cout << endl; cout << num1 << " - " << num2 <<" = "<< (num1-num2) << endl; cout << endl; mainmenu(); } void multiplication() //do multiplication function. { cout << endl; cout << "Enter a number to multiply: " << endl; cin >> num1 ; cout << "Enter a second number to multiply by: " << endl; cin >> num2 ; cout << endl; cout << num1 << " X " << num2 <<" = "<< (num1*num2) << endl; cout << endl; mainmenu(); } void division() // do division function. { cout << endl; cout << "Enter a number to divide: " << endl; cin >> num1 ; cout << "Enter a number to divide by: " << endl; cin >> num2 ; cout << endl; cout << num1 << " / " << num2 << " = " << (num1/num2) << endl; cout << endl; mainmenu(); } void mainmenu() { cout << endl; cout << "*** Main Menu ***" << endl; cout << endl; cout << "Please choose from the following, press [5] to quit: " << endl; //main option cout << endl; /*main menu options */ cout << " Press [1] for Addition" << endl; cout << " Press [2] for Subtraction" << endl; cout << " Press [3] for Multiplication" << endl; cout << " Press [4] for Division" << endl; cout << endl; cin >> choice; //ask user to enter selection, places it in int choice switch (choice) { case 1: addition(); break; case 2: subtraction(); break; case 3: multiplication(); break; case 4: division(); break; case 5: (start = false); int main(); cout<<"Exiting..."<<endl<<endl; break; default: cout << " INVALID ENTER, Try again..." << endl << endl; cin.clear(); cin.sync(); mainmenu(); } }Explanation / Answer
Here is the code (Blue Highlighter Indicates a change, Green Highlighter is a new line)... I think I highlighed all the important changes...
// Author: Me
// BCS 120
// Description: C++ Math Tutor
#include <iostream> //Header Declaration
#include <iomanip>
#include <cstdlib> //Used for a better random number generator
#include <time.h> //Used for a better random number generator
using namespace std;
int ans1, num1, num2; // declare num1 and num2
int choice; // variable choice
void mainmenu(); // Main menu
void addition(int rand1, int rand2); // addition function
void subtraction(int rand1, int rand2); // subtraction function
void multiplication(int rand1, int rand2); // multiplication function
void division(int rand1, int rand2); // division function
bool start = true; // start
int main()
{
if (start = true)
mainmenu ();
else if (start = false);
system("pause");
return 0;
}
void addition(int rand1, int rand2) // perform addition.
{
/* Initialize some var's and set equal to our passed values */
int random1 = rand1;
int random2 = rand2;
cout << "What is " << random1 << " + " << random2 << " = ?" << endl << endl;
cin >> ans1;
cout << "You answered that " << random1 << " + " << random2 << " is equal to: " << ans1 << endl;
if( ans1 == ( random1 + random2 ) )
{
cout << "Congratulations..You are Correct" << endl;
}
else
{
cout << "Incorrect..The correct answer is..." << random1 << " + " << random2 << " = " << ( random1 + random2 ) << endl;
}
mainmenu();
}
void subtraction(int rand1, int rand2) //do subraction function.
{
/* Initialize some var's and set equal to our passed values */
int random1 = rand1;
int random2 = rand2;
cout << "What is " << random1 << " - " << random2 << " = ?" << endl << endl;
cin >> ans1;
cout << "You answered that " << random1 << " - " << random2 << " is equal to: " << ans1 << endl;
if( ans1 == ( random1 - random2 ) )
{
cout << "Congratulations..You are Correct" << endl;
}
else
{
cout << "Incorrect..The correct answer is..." << random1 << " - " << random2 << " = " << ( random1 - random2 ) << endl;
}
mainmenu();
}
void multiplication(int rand1, int rand2) //do multiplication function.
{
/* Initialize some var's and set equal to our passed values */
int random1 = rand1;
int random2 = rand2;
cout << "What is " << random1 << " X " << random2 << " = ?" << endl << endl;
cin >> ans1;
cout << "You answered that " << random1 << " X " << random2 << " is equal to: " << ans1 << endl;
if( ans1 == ( random1 * random2 ) )
{
cout << "Congratulations..You are Correct" << endl;
}
else
{
cout << "Incorrect..The correct answer is..." << random1 << " X " << random2 << " = " << ( random1 * random2 ) << endl;
}
mainmenu();
}
void division(int rand1, int rand2) // do division function.
{
/* Initialize some var's and set equal to our passed values */
int random1 = rand1;
int random2 = rand2;
cout << "What is " << random1 << " / " << random2 << " = ?" << endl << endl;
cin >> ans1;
cout << "You answered that " << random1 << " / " << random2 << " is equal to: " << ans1 << endl;
if( ans1 == ( random1 / random2 ) )
{
cout << "Congratulations..You are Correct" << endl;
}
else
{
cout << "Incorrect..The correct answer is..." << random1 << " / " << random2 << " = " << ( random1 / random2 ) << endl;
}
mainmenu();
}
void mainmenu()
{
/* Advamced Random Number Generation (Better-Results) */
int random1, random2;
time_t seconds; /* Declare variable to hold seconds on clock. */
time( &seconds ); /* Get value from system clock and place in seconds variable. */
srand( ( unsigned int ) seconds ); /* Convert seconds to a unsigned integer. */
random1 = rand() % (20 - 1 + 1) + 1;
random2 = rand() % (20 - 1 + 1) + 1;
/* Standard Random Number Generation (Ok-Results) */
//int random1 = rand() % 20+1; // Random number will start at 0 so add one
//int random2 = rand() % 20+1; // Random number will start at 0 so add one
cout << endl << "*** Main Menu ***" << endl << endl;
cout << "Please choose from the following, press [5] to quit: " << endl << endl;
/*main menu options */
cout << " Press [1] for Addition" << endl;
cout << " Press [2] for Subtraction" << endl;
cout << " Press [3] for Multiplication" << endl;
cout << " Press [4] for Division" << endl;
cout << endl;
cin >> choice; //ask user to enter selection, places it in int choice
switch (choice)
{
case 1: addition(random1, random2);
break;
case 2: subtraction(random1, random2);
break;
case 3: multiplication(random1, random2);
break;
case 4: division(random1, random2);
break;
case 5:
(start = false);
int main(); cout << "Exiting..." << endl << endl;
break;
default:
cout << " INVALID ENTER, Try again..." << endl << endl;
cin.clear();
cin.sync();
mainmenu();
}
}
Sample Output:
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
1
What is 2 + 8 = ?
10
You answered that 2 + 8 is equal to: 10
Congratulations..You are Correct
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
1
What is 15 + 1 = ?
16
You answered that 15 + 1 is equal to: 16
Congratulations..You are Correct
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
2
What is 10 - 5 = ?
5
You answered that 10 - 5 is equal to: 5
Congratulations..You are Correct
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
4
What is 19 / 19 = ?
1
You answered that 19 / 19 is equal to: 1
Congratulations..You are Correct
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
3
What is 3 X 5 = ?
15
You answered that 3 X 5 is equal to: 15
Congratulations..You are Correct
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
3
What is 6 X 6 = ?
12
You answered that 6 X 6 is equal to: 12
Incorrect..The correct answer is...6 X 6 = 36
*** Main Menu ***
Please choose from the following, press [5] to quit:
Press [1] for Addition
Press [2] for Subtraction
Press [3] for Multiplication
Press [4] for Division
5
Exiting...
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.