Question to generates random math problems.

I was asked to write a c++ program to generates random math problems.
Here's my code so far. It works, but it's not the result that i want. Right now it only generates random numbers for the math problem, but what should i do to make it generates random math problems( addition, subtraction, and, multiplication) each time it runs ??
Please help..



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

int main ()

{ int MAX = 10;
int right_number;
int a;
int b;

srand(time (0));
a = 1 + rand() % MAX;
b = 1 + rand() % MAX;

cout <<"please add these two numbers"<<endl;
cout <<" "<<endl<<endl<<endl;
cout <<" "<< a <<endl;
cout <<" "<< b <<endl;
cout <<"+____"<<endl;

cout <<"what is the correct answer?";
cin >>right_number;

while (right_number != (a + b))
{
cout <<"Sorry, that's not it." << endl;

if (right_number < (a + b))
cout << "Try again.." << endl;
else
cout << "Try again.." << endl;
cin >> right_number;

} // endwhile

cout << "Your answer is correct!" << endl;

return 0;

}
How about this:

1) seed rand like you have.

2) create two variables and assign them random numbers

3) create an array with 4 elements e.g. { '+', '-', '*', '/' };

4) Randomly chose an element from this array.

Then you'll have a random math problem and an easy way to solve it - use a switch statement.
Last edited on
Thank you very much!
No problem :) Please mark as solved.
That's a pretty good idea, actually.
Topic archived. No new replies allowed.