math tutor program

Feb 14, 2015 at 2:29am
This is my code for so far. How would i add a function to allow the user to select the type of math problem ( addition, subtraction , multiplication, division ) ?



#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;

int main ()
{
unsigned seed = time(0);

int num1, num2, student_answer, correct_answer;
srand(seed);


num1 = 1 + rand() % 10;
num2 = 1 + rand() % 10;

correct_answer = num1 + num2;

cout << num1 << endl;
cout << "+" << endl;
cout << num2 << endl;
cout << "Please enter your answer " << endl;
cin >> student_answer;
cout<< "the correct answer is " << correct_answer<< endl;

return 0;
}
Feb 14, 2015 at 6:33am
What specifically are you struggling with?
Feb 14, 2015 at 7:06am
How would i add a functionality that allow the user to select the type of problem (addition, subtraction, multiplication, division). Then, show the user their problem, allow them to enter the answer, and then give them a message showing congratulations if they have answered correctly and a separate message if they did not (including the correct answer)
Feb 14, 2015 at 6:08pm
Tackle one problem at a time. Start with the first thing: having the user select what they want.
Feb 14, 2015 at 10:07pm
I think, for that, we can ask the user to "cin" an operator (a char) and use if-else or switch-case operations to select the operation to perform.
Feb 14, 2015 at 10:24pm
I tried that but kept getting an error. I'm probably doing it incorrect. Any idea in how to do it?
Feb 14, 2015 at 10:26pm
Something like:
1
2
3
4
5
6
7
8
9
char chOperator;
cout << "Enter an operator: ";
cin >> chOperator;
int nResult = 0;
switch(chOperator)
case '+':
   nResult = num1 + num2;
case '-':
   nResult = num1 - num2;

and so on!
Topic archived. No new replies allowed.