Math tutor

Problem: Write a C++ program that uses a single DO…WHILE loop structure to create a
math tutor for young students
Display a menu offering the user the option of performing addition, subtraction, multiplication,
or division (use a “switch”). Create separate functions for addition, subtraction,
multiplication, and division. The final selection on the menu should allow the user to exit the
program. The menu is to be displayed again after the user has finished each problem. Use the
“rand” function to display two random numbers to be used in each problem (see NOTE below).
The program should then pause while the student works on the problem and enters his/her
answer. When the student enters his/her answer and presses the “Enter” key, the program will
display the correct solution. If the answer is correct, the program will display a message of
congratulations. If the answer is incorrect, the program will display a message along with the
correct answer.
When the program ends, display a message “thanking” the user for using the Math Tutor
program. Use appropriate output formatting features to make your output line up as in the
sample output below.
NOTE: For addition, generate random numbers in the range 1 – 500.
For subtraction generate random numbers in the range 1 – 999
(the second number must be less than the first number).
For multiplication generate random numbers in the range 1 – 100 for the first
number and generate random numbers in the range 1 – 9 for the multiplier.
For division generate random numbers in the range 1 – 9 for the divisor; generate
random numbers in the range generate random numbers in the range 1 – 50 for
the numerator and then multiply the random number by the divisor so that the
result is always a whole (integer) number.
Information about the rand() function can be found on pages 129-130 in the text.
The shorthand (or abbreviated) version is: srand(time(0));
If you include some of your source code, I can gladly help you. This is just pseudo-code or your asking me to do it.
Could you please do it for me and maybe include short statements on what each part does.I'm totally stuck
No, I can not do it for you. Show me what you have so far and I will proceed to explain how to finish it. You might want to use a random seed to get your numbers.
I did something like this not too long ago (I am also learning). You should really try and figure it out on your own because if you can't then you didn't try hard enough. As a tip heres one way you could do multiplication.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void multiplicationFunc(string test2)
{
	srand(time(0));
	int answer;
	int n1 = rand() % 100 + 1;
	int n2 = rand() % 9 + 1;

	cout << "-------------------\n"
		<< "-------------------\n"
		<< "| What is " << n1 << " X " << n2 << " ? |\n"
		<< "-------------------\n"
		<< "-------------------\n";

	answer = validateNumber(); //If you don't have to validate then cin >> answer;

	if (answer == n1 * n2)
	{
		system("cls");
		isCorrect(test2); //A function that assigns points if it is correct.
		cout << "     ****** Correct! *******\n";
	}
	else
	{
		system("cls");
		isWrong(test2); //A function that assigns points if it is wrong.
		cout << "     ****** Wrong! ******\n";
	}
}


You are literally asking someone else to do it for you though, which would defeat the whole purpose of you learning C++ anyways.

P.S The reason i have a "isWrong" and "isCorrect" function was because for my project I had to assign or take away points from the user and that's one way I went about it.
Last edited on
Here's what i came up with

//This program will be used to help tutor math students
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
#define IDLINE "-CS1336-Bonus lab\n"
int num1, // The first random number
num2, // The second random number
choice, // The user's choice of problem
studentAnswer, // The student's answer
correctAnswer; // The correct answer

//Display the ID line
cout << IDLINE << endl ;

// Seed the random number generator.
srand(time(0));

do
{ // Display the menu and get a choice.

cout << "\tMath Tutor Menu\n";
cout << "------------------------------\n";
cout << "1. Addition problem\n";
cout << "2. Subtraction problem\n";
cout << "3. Multiplication problem\n";
cout << "4. Division problem\n";
cout << "5. Quit this program\n";
cout << "------------------------------\n";
cout << "Enter your choice (1-5): ";
cin >> choice;

// Validate the choice.
while (choice < 1 || choice > 5)
{
cout << "The valid choices are 1, 2, 3, "
<< "4, and 5. Please choose: ";
cin >> choice;
}

// Produce a problem.
switch (choice)
{
case 1: // Addition problem
// Generate two random numbers in
// the range 1 - 500.
num1 = 1 + rand() % 500;
num2 = 1 + rand() % 500;

// Calculate the correct answer.
correctAnswer = num1 + num2;

// Display the problem.
cout << "\n\n";
cout << " " << setw(4) << num1 << endl;
cout << " +" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
break;

case 2: // Subtraction problem
// Generate two random numbers in
// the range 1 - 999.
num1 = 1 + rand() % 999;
num2 = 1 + rand() % 999;

// Make sure num2 <= num1...
while (num2 > num1)
num2 = 1 + rand() % 999;

// Get the correct answer.
correctAnswer = num1 - num2;

// Display the problem.
cout << "\n\n";
cout << " " << setw(4) << num1 << endl;
cout << " -" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
break;

case 3: // Multiplication problem
// Generate two random numbers. The first in
// the range 1 - 100, the second in the
// range 1 - 9.
num1 = 1 + rand() % 100;
num2 = 1 + rand() % 9;

// Calculate the correct answer.
correctAnswer = num1 * num2;

// Display the problem.
cout << "\n\n";
cout << " " << setw(4) << num1 << endl;
cout << " *" << setw(4) << num2 << endl;
cout << " " << "----" << endl;
cout << " ";
break;

case 4: // Division problem with no remainder
// Generate a single digit divisor.
num2 = 1 + rand() % 9;

// Generate a number that is a multiple
// of num2...
num1 = num2 * (rand() % 50 + 1);

// Calculate the correct answer.
correctAnswer = num1 / num2;

// Display the problem.
cout << "\n\n";
cout << " " << num1 << " / " << num2 << " = ";
break;

case 5: // The user chose to quit the program.
cout << "Thank you for using Math Tutor.\n\n";
break;
}

// If student selected a problem, get and evaluate the answer.
if (choice >= 1 && choice <= 4)
{
cin >> studentAnswer;
if (studentAnswer == correctAnswer)
cout << "\n\nCongratulations! That's right.\n\n";
else
cout << "\n\nSorry, the correct answer is " << correctAnswer
<< ".\n\n";
}
} while (choice != 5); // Loop again if student did not choose to quit.
return 0;
}
Last edited on
This seems to be working fine, what was your problem?
Topic archived. No new replies allowed.