Hi im trying to make a maths program and im having trouble with one of my sections where im currently just using '+' incorporated in to a do while loop until the score = 10 though i'm stuck on how to make it randomly change the question between + or - as so far i've only got +
That looks like a fun little program. To make it do both addition and subtraction, you need to determine what changes between the two paths. Edward sees that you need to display a different character for the equation--either '+' or '-' so the user knows how to figure the answer--and the answer itself needs to reflect the right operation. If you hoist the operator character out into a variable, you can localize the choice between addition and subtraction without changing the rest of the code:
cout << "Incorrect, the answer is " << rhs << '\n';
}
}
The choice is made by going one way if the random value is even, and the other way if it's odd. That gives you a good 50/50 chance for either addition or subtraction. You can change that so one has more probability than the other. For example, if you change rand() % 2 to rand() % 3, addition will be twice as likely.
This approach to making random decisions can be applied in a lot of places and can be extended for more than two paths. It's a good trick to have under your belt.
Last edited by Radical Edward; Aug 18th, 2008 at 11:41 am.
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.