943,569 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 500
  • C++ RSS
Oct 21st, 2008
-1

Looping question

Expand Post »
The following program is a game in which there is a pile of coins. When the loop runs two players take turns removing coins. Whoever gets the last coin wins the game. ... How can I get the looping to stop when there is a winner?? Right now it still displays the "player 1 please remove coins" output...
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5. {
  6. double pileSize;
  7. double maxTurn;
  8.  
  9. double turn;
  10. double removed;
  11. double left;
  12.  
  13. cout << "Enter the size of the pile: ";
  14. cin >> pileSize;
  15.  
  16. cout << "Enter the maximum size of the turn: ";
  17. cin >> maxTurn;
  18.  
  19. while (pileSize > 0)
  20. {
  21. cout << "Player 1, it is your turn to remove chips!"; //Player 1's turn
  22. cin >> turn;
  23.  
  24. if (turn > maxTurn || turn < 1) //Validity
  25. {
  26. cout << "Invalid turn, please retry... ";
  27. cin >> turn;
  28. }
  29.  
  30. removed += turn; //Keep a running total of turns
  31. left = pileSize - removed; //Subtract total from pile
  32.  
  33. cout << "There are " << left << " chips remaining."; //Display remaining in pile
  34. if (left < 1)
  35. cout << " Player 1, You have one the game!" << endl; //Player 1 wins game! if there are 0 chips left
  36. else
  37. { //if there are chips remaining, another turn
  38. cout << " Player 2, it is your turn to remove chips!";
  39. cin >> turn;
  40.  
  41.  
  42. if (turn > maxTurn || turn < 1) //validity
  43. {
  44. cout << " Invalid turn, please retry... ";
  45. cin >> turn;
  46. }
  47.  
  48. removed += turn; //keep running total
  49. left = pileSize - removed; //display remaining
  50. cout << "There are " << left << " chips remaining."; //if there are no chips left, player 2 wins
  51. if (left < 1)
  52. cout << " Player 2, you have won the game! " << endl;
  53. }
  54.  
  55. }
  56.  
  57. return 0;
  58. }
Last edited by cscgal; Oct 21st, 2008 at 4:57 pm. Reason: Added code tags
Similar Threads
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008
Oct 21st, 2008
0

Re: Looping question

There are a number of ways to get out of the loop. One way is to put the break statement when someone wins the game:
C++ Syntax (Toggle Plain Text)
  1. if (left < 1)
  2. {
  3. cout << " Player 1, You have one the game!" << endl;
  4. break;
  5. }

Else, you can set a flag to true whenever someone wins the game, and then put the check for the flag as one of the condition of the while-loop.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 21st, 2008
0

Re: Looping question

There are some more problems with your codes:

1. You never initialized the variable "removed", yet you used it to keep track of the turns:
C++ Syntax (Toggle Plain Text)
  1. removed += turn; //Keep a running total of turns

This will lead to unpredictable behaviour.

2. Why use double for all the variables? Since you are dealing with discrete items like coins, you should use integer instead.

3. You should use while-loop for your validity checks. Using if-statement will only check once.
Reputation Points: 118
Solved Threads: 15
Posting Pro in Training
Denniz is offline Offline
428 posts
since Sep 2008
Oct 21st, 2008
0

Re: Looping question

Ok, that makes sense thank you!
Reputation Points: 4
Solved Threads: 0
Light Poster
cproud21 is offline Offline
42 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: counting number (some suggestions needed!)
Next Thread in C++ Forum Timeline: Help whit this error LNK1169





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


Follow us on Twitter


© 2011 DaniWeb® LLC