For C++: Programming Challenge 15 of Chapter 3: Math Teacher : Write a program t
ID: 3875340 • Letter: F
Question
For C++:
Programming Challenge 15 of Chapter 3:
Math Teacher :
Write a program that can be used as a math teacher for a young student. The program should display two random numbers to be added, such as 247 +129 The program should then pause while the student works on the problem. When the student is ready to check the answer, he or she can press a key and the program will display the correct solution: 247 +129= 376
Programming Challenge 9 of Chapter 4:
Math Teacher:
This is a modification of Programming Challenge 15 from Chapter 3 . Write a program that can be used as a math teacher for a young student. The program should display two random numbers that are to be added, such as: 247 + 129 The program should wait for the student to enter the answer. If the answer is correct, a message of congratulations should be printed. If the answer is incorrect, a message should be printed showing the correct answer.
This program started in Programming Challenge 15 of Chapter 3, and was modified in Programming Challenge 9 of Chapter 4. Modify the program again so it displays a menu allowing the user to select an addition, subtraction, multiplication, or division problem. The final selection on the menu should let the user quit the program. After the user has finished the math problem, the program should display the menu again. This process is repeated until the user chooses to quit the program. Input Validation: If the user selects an item not on the menu, display an error message and display the menu again. Numbers should be two digits long. Division problems should have even answers.
Explanation / Answer
#include <iostream> //imports libraries.
#include <cstdlib>
#include <ctime>
using namespace std;
void displayResult(int result){
int response;
cin >> response; //takes user input ans as response.
if (response == result){
cout<<"Congratulations. ";
}
else{
cout<<"Correct Ans: "<<result<<" ";
cin.clear(); //incase the input by user is a non integer value, which will cause cin to go in failed state as cin is assigned to integer.
cin.ignore(50, ' '); //asks cin to discard 50 characters from the input stream.
}
}
void addition(int num1, int num2){
int result;
result = num1 + num2; //performs addition of two numbers.
displayResult(result);
}
void subtraction(int num1, int num2){
int result;
result = num1 - num2; //performs subtraction of two numbers.
displayResult(result);
}
void multiplication(int num1, int num2){
int result;
result = num1 * num2; //performs multiplication.
displayResult(result);
}
void division(int num1, int num2){
int result;
result = num1 / num2; //performs division.
displayResult(result);
}
void displayMenu(){
int number_1, number_2,max_num = 99, min_num = 10, choice; //initilizing and declaring variables.
srand(time(0)); //The srand() function sets the starting point for producing a series of pseudo-random integers.
number_1 = rand() % (max_num + 1 - min_num) + min_num; //generating a random no. between max and min value(range).
number_2 = rand() % (max_num + 1 - min_num) + min_num;
cout<<"**************************************** ";
cout<<"Enter a Choice: ";
cout<<"1. Addition ";
cout<<"2. Subtraction ";
cout<<"3. Multiplication ";
cout<<"4. Division ";
cout<<"5. Exit ";
cout<<"**************************************** ";
cin>>choice; //takes user choice input from the menu.
switch(choice){ //switch case for user choice of options.
case 1: cout <<"1st Number: " <<number_1<<" 2nd Number: "<<number_2;
cout<<" Enter ans: ";
addition(number_1, number_2);
break;
case 2: cout <<"1st Number: " <<number_1<<" 2nd Number: "<<number_2;
cout<<" Enter ans: ";
subtraction(number_1, number_2);
break;
case 3: cout <<"1st Number: " <<number_1<<" 2nd Number: "<<number_2;
cout<<" Enter ans: ";
multiplication(number_1, number_2);
break;
case 4: cout <<"1st Number: " <<number_1<<" 2nd Number: "<<number_2;
cout<<" Enter ans: ";
division(number_1, number_2);
break;
case 5: exit(0);
break;
default: cout<< "Invalid Input! ";
cin.clear();
cin.ignore(50, ' ');
break;
}
}
int main() //main function runs the program.
{
while(true){ //while loop executes untill exit signal is recieved.
displayMenu(); //call to displayMenu function.
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.