My output for my assignment is supposed to be something like this:
What is 10+5=? 7
Incorrect: 10+5=15
What is 9-4=? 5
Correct!
(etc)
Your total score is ?
I have to ask a student for the answer to 10 random arithmetic problems consisting of addition and subtraction using integers from 1 to 10 only. Pick two random integers from 1 to 10 and assign to num1 and num2 and pick a random integer 0 and 1 for the the operation (OP). If OP is 0 it is addition, 1 is subtraction. If it is a subtraction you must make sure that the first number Num1 is bigger than the second number Num2. If it is not then swap the numbers by calling a function Swap that you will define. Each correct problem is awarded 10 points. Display the total score at the end.
My program isn't doing that, I'm not sure what I'm doing wrong in my code.
Your swap function isn't doing what you want. If you pass in the numbers by value, they will not reflect the change when the function returns to main() You need to take 2 numbers (references, so they will remain after function return) and swap their values:
1 2 3 4 5 6
void swap(int &n1, int &n2)
{
int tmp = n1;
n1 = n2;
n2 = tmp;
}
You also need to actually check if num1 is greater than num2 before you assign correctAns.
1 2
if(num1 < num2) swap(num1,num2);
correctAns=num1-num2; // because of the above num1 will always be > num2