| | |
Random Change Between
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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 +
thankyou
C++ Syntax (Toggle Plain Text)
int choice ; cin >> choice; do { int r, r2, answerplus, answersubtract; r = rand() % 8 + 1; r2 = rand() % 8 + 1; answerplus = r + r2; out << "question number " << questionumber << endl; cout << r << "+ __ = " << answerplus << endl; int responce; cin >> responce; if (responce == r2) {cout << "correct!" << endl; score = score + 1; } else {cout << "wrong!" << endl; } questionumber = questionumber + 1; cout << "your score is " << score << endl; while (questionumber <= 10);
thankyou
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:
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.
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: C++ Syntax (Toggle Plain Text)
#include <cstdlib> #include <ctime> #include <iostream> int main() { using namespace std; srand(unsigned(time(0))); while (true) { int lhs = rand() % 8 + 1; int rhs = rand() % 8 + 1; int answer; char op; // Choose equally between + and - if (rand() % 2 == 0) { answer = lhs + rhs; op = '+'; } else { answer = lhs - rhs; op = '-'; } cout << lhs << ' ' << op << " __ = " << answer << "\n>"; int choice; if (!(cin >> choice)) break; if (choice == rhs) cout << "Correct!\n"; else cout << "Incorrect, the answer is " << rhs << '\n'; } }
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.
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- Forms in Random access files (Visual Basic 4 / 5 / 6)
- Random Salary (Java)
- Change System wide display color profile? (OS X)
- Monitor error: Unable to change settings. Flickering colours. (Monitors, Displays and Video Cards)
- Random Shutdowns/Restarts (Windows NT / 2000 / XP)
- Random restarts and Much More (Troubleshooting Dead Machines)
- Wierd internet connectivity problem- Random sites have become inaccessible (Networking Hardware Configuration)
- Random popups, even when not online (Windows NT / 2000 / XP)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: Help with this Link list
- Next Thread: Can't use Strings :'(
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






