simple math program

Im trying to write a program to allow a grade to be entered then present a math problem of varying difficulty. The higher the grade the harder the problems are. First grade is only addition, but second is addition and subtraction. I am completely spacing on how to set up my code to make the number being subtracted from be on top.

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

string name;
double a, b, c;

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

cout <<"Welcome to the Math Tutor\n\n";

getline(cin, name);

a = rand() % 10;
b = rand() % 10;
c = a + b;
cout <<"Problem: ";
cout << right << setw(5);
cout << a << endl;
cout << right << setw(13);
cout <<"+ " << b << endl;
cout << right << setw(14);
cout <<"----"<< endl;
cout << left;
}
else if (grade == 2) {
a = rand() % 89 + 10;
a = rand() % 89 + 10;}

}

Here is my code so far. I havent completed the second if yet because I dont know how to word it. Yes, this is a homework assignment, but I am not looking for a complete solution, only a push in the right direction. Like i said i need to allow for either addition or subtraction with the higher number being above the lower. Am i right in using if statements? Or should I be using switch or some other function? New to programming this semester, and our teacher is not very good. Thanks for any help you can give.
Last edited on
As far as using the else/else-if statements there is absolutely nothing wrong with what you have done. I'm assuming you have run it through a compiler and it runs okay.
You can use a switch statement but it offers nothing better than an if/else if statement, it is just personal preference. I like ifs better but it doesn't harm you to known the switch method if you want to look into it.
The other option you could go for is to have functions that you invoke outside of your main function, but this is more useful when you have code that repeats itself, so unless you were to call the same question twice within one run of the program.
For a beginner to c++ I think you are doing this in a fine way. I'm assuming afterwards you will check the users answer to the value of c, but I will give you the benefit of the doubt as this isn't finished yet :P

As for the problem with the two numbers, I'm assuming this means you want 'a' to be the higher number than 'b' at all times.

After you get a value for both a and b you can call this if statement

 ``12345`` ``````if (b > a){ double temp = b; b = a; a = temp; }``````

This will ensure that a is always the bigger of the two numbers, you don't have to worry about generating numbers until b is a smaller value

I hope this is what you wanted, if not post back =]
That's pretty much what I wanted. Do I place the if statement after I declared the a/b random assignment statements? Also, how to I make it either a subtraction or addition problem? Is there something I can do with the random function or do I have to write another else-if statement inside the function? I dont know why this subject is so difficult for me to grasp. Thanks for the help.
Yes you would put that if statement after you have applied a value to a and b. If you try to do it before assigning the values it might get messy.

To determine whether to do an add or subtract, if it is on an even chance you could try calling something like

 ``12345678`` ``````int question = rand()%2; if (question == 0){ //Write out addition sum here } else{ //Write out subtraction sum here }``````
So i put in the code muckytears recommended but now the problem I am having is if b != || > a, then how do I get the subtraction problem to print out using if/else statements. I think I might just have a logic error. The code I tried is the else statement that I commented out. Sorry for the sloppy code I dont know how to put the code on here like everyone else does with a nano editor. The code I have compiles and runs except that if b isnt greater than a, the program just skips all the code and doesnt print the problem. Thanks for any help.

else if (grade == 2) {
a = rand() % 89 + 10;
b = rand() % 89 + 10;
int question = rand() % 2;
if (question == 0) {
c = a + b;
cout <<"Problem: ";
cout << right << setw(5);
cout << a << endl;
cout << right << setw(12);
cout << "+ " << b << endl;
cout << right << setw(14);
cout << "----" << endl;
cout << "\nCongratualations " << name << ", you are correct!";
}//end if
else { cout << "\nSorry " << name << ", you are incorrect. The correct answer was " << c << "." << endl;
}//end else
}//end if
}//end else if

else if ( b > a) {
double temp = b;
b = a;
a = temp;
c = a - b;
cout << "Problem: ";
cout << right << setw(5);
cout << a << endl;
cout << right << setw(12);
cout << "- " << b << endl;
cout << right << setw(14);
cout << "----" << endl;
/* else { c = a - b;
cout << "Problem: ";
cout << right << setw(5);
cout << a << endl;
cout << right << setw(12);
cout << "- " << b << endl;
cout << right << setw(14);
cout << "----" << endl;
}*/
cout << "\nCongratualations " << name << ", you are correct!";
}//end if
else { cout <<"\nSorry " << name << ", you are incorrect. The correct answer was " << c << "." << endl;
}//end else
} //end else if