Looping question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Looping question

 
-1
  #1
Oct 21st, 2008
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...
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: Looping question

 
0
  #2
Oct 21st, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 429
Reputation: Denniz is on a distinguished road 
Solved Threads: 15
Denniz's Avatar
Denniz Denniz is offline Offline
Posting Pro in Training

Re: Looping question

 
0
  #3
Oct 21st, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Re: Looping question

 
0
  #4
Oct 21st, 2008
Ok, that makes sense thank you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC