| | |
Looping question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 38
Reputation:
Solved Threads: 0
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)
#include <iostream> using namespace std; int main () { double pileSize; double maxTurn; double turn; double removed; double left; cout << "Enter the size of the pile: "; cin >> pileSize; cout << "Enter the maximum size of the turn: "; cin >> maxTurn; while (pileSize > 0) { cout << "Player 1, it is your turn to remove chips!"; //Player 1's turn cin >> turn; if (turn > maxTurn || turn < 1) //Validity { cout << "Invalid turn, please retry... "; cin >> turn; } removed += turn; //Keep a running total of turns left = pileSize - removed; //Subtract total from pile cout << "There are " << left << " chips remaining."; //Display remaining in pile if (left < 1) cout << " Player 1, You have one the game!" << endl; //Player 1 wins game! if there are 0 chips left else { //if there are chips remaining, another turn cout << " Player 2, it is your turn to remove chips!"; cin >> turn; if (turn > maxTurn || turn < 1) //validity { cout << " Invalid turn, please retry... "; cin >> turn; } removed += turn; //keep running total left = pileSize - removed; //display remaining cout << "There are " << left << " chips remaining."; //if there are no chips left, player 2 wins if (left < 1) cout << " Player 2, you have won the game! " << endl; } } return 0; }
Last edited by cscgal; Oct 21st, 2008 at 4:57 pm. Reason: Added code tags
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:
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.
C++ Syntax (Toggle Plain Text)
if (left < 1) { cout << " Player 1, You have one the game!" << endl; break; }
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.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
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:
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.
1. You never initialized the variable "removed", yet you used it to keep track of the turns:
C++ Syntax (Toggle Plain Text)
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.
Founder of :
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
Lexel Technologies Pte Ltd - SMS (TXT) Marketing software solution
My Blogs: Gooner's Sanctuary
Pet Directory and Forum: FurryTale.net
![]() |
Similar Threads
- Write a program that utilizes looping to print the following table of values: using C (C)
- Beginner Question..? (PHP)
- A question about SCASB, using it in decoding. (Assembly)
- for loop question (C++)
- Question: Linear Time Sorting Problem (Computer Science)
- Looping Assignment- Need Help!! (Java)
- VBA Macro Coding Question (Visual Basic 4 / 5 / 6)
- RSS Feeds Question (PHP)
- looping and spaces (C)
Other Threads in the C++ Forum
- Previous Thread: counting number (some suggestions needed!)
- Next Thread: Help whit this error LNK1169
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game 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 number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





