943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 501
  • C++ RSS
Aug 18th, 2008
0

Random Change Between

Expand Post »
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 +


C++ Syntax (Toggle Plain Text)
  1. int choice ;
  2. cin >> choice;
  3. do {
  4. int r, r2, answerplus, answersubtract;
  5. r = rand() % 8 + 1;
  6. r2 = rand() % 8 + 1;
  7. answerplus = r + r2;
  8. out << "question number " << questionumber << endl;
  9. cout << r << "+ __ = " << answerplus << endl;
  10.  
  11. int responce;
  12. cin >> responce;
  13.  
  14. if (responce == r2) {cout << "correct!" << endl;
  15. score = score + 1; }
  16. else {cout << "wrong!" << endl; }
  17. questionumber = questionumber + 1;
  18. cout << "your score is " << score << endl;
  19. while (questionumber <= 10);

thankyou
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ichigoSJ is offline Offline
3 posts
since Mar 2008
Aug 18th, 2008
0

Re: Random Change Between

I think you need to describe what it is your making and what your is problem a little better. Simply saying i'm stuck isn't enough.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Aug 18th, 2008
0

Re: Random Change Between

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:
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. srand(unsigned(time(0)));
  10.  
  11. while (true) {
  12. int lhs = rand() % 8 + 1;
  13. int rhs = rand() % 8 + 1;
  14. int answer;
  15. char op;
  16.  
  17. // Choose equally between + and -
  18. if (rand() % 2 == 0) {
  19. answer = lhs + rhs;
  20. op = '+';
  21. }
  22. else {
  23. answer = lhs - rhs;
  24. op = '-';
  25. }
  26.  
  27. cout << lhs << ' ' << op << " __ = " << answer << "\n>";
  28.  
  29. int choice;
  30.  
  31. if (!(cin >> choice))
  32. break;
  33.  
  34. if (choice == rhs)
  35. cout << "Correct!\n";
  36. else
  37. cout << "Incorrect, the answer is " << rhs << '\n';
  38. }
  39. }
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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is more than three months old

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.
Message:
Previous Thread in C++ Forum Timeline: Help with this Link list
Next Thread in C++ Forum Timeline: Can't use Strings :'(





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC