# Help with "while loop"

• 11-07-2010
sergiosergio
Help with "while loop"
PART 1) Add the appropriate Boolean expression to the while. The while loop should exit when a team has scored at least 15 points and is ahead by at least 2. That is, the loop exits when the score is 15 – 8 but not 15 -14. Remember that the Boolean expression should be the opposite of the exit condition. Hint: A good strategy in this case is to write a Boolean expression for the stopping condition and apply the NOT operator, !, to it. (PART 2) Also program should play multiple games, wins match at 5 game wins with a difference of at least two games.

I'm having trouble with the second second part 2. How do i get it to play the multiple games and the winner should win by at least two games? As it stands right now, when it compiles, it only lets me play the game 1 time.

This is what i have so far; (Any help would be appreciated)

Code:

```#include <cstdlib> #include <iostream> using namespace std; const int STOPSCORE=15; int main(int argc, char *argv[]) { int nyu,GAMESPLAYED,gamecounter,game,game3,g… usc; char team; // either n or u nyu=10; usc=10; GAMESPLAYED=0; while (GAMESPLAYED < 5) { cout <<GAMESPLAYED; GAMESPLAYED++; while ((nyu < STOPSCORE && usc < STOPSCORE) ||(nyu < usc +2)&&(usc < nyu+2)) { cout << "score starts from 5. "; cout << "Who scored the point? "; cin >> team; // increment the appropriate score switch (team) { case 'n': case 'N': nyu++; break; case 'u': case 'U': usc++; break; default: cout <<"invalid entry"<<endl; cout << "Current Score: nyu: " << nyu << " usc: " << usc << endl; } // print final score if (usc > nyu) cout << "\nBoo, usc: " << usc << " nyu: " << nyu; else cout << "\nYeah, nyu: " << nyu << " usc: " << usc; cout << "\n\n"; } } system("PAUSE"); return EXIT_SUCCESS; }```
• 11-07-2010
sergiosergio
Ok, I figured out how to make the program play 5 games but now i'm stuck on how to make one team win by at least 2 games. Im new to this so any help would be appreciated.

This is my updated code:

Code:

```#include <cstdlib> #include <iostream> using namespace std; const int STOPSCORE=15; int main(int argc, char *argv[]) {         int nyu,GAMESPLAYED,usc;         char team;         // either n or u nyu=10; usc=10; GAMESPLAYED=1;         while ((GAMESPLAYED < 6) {         cout << " Game score starts from 5 ";         cout <<endl;         cout << "Game # "<< GAMESPLAYED<< " ....";         GAMESPLAYED++; nyu=10; usc=10;          while ((nyu < STOPSCORE && usc < STOPSCORE)     ||(nyu < usc +2)&&(usc < nyu+2)) {                            cout << "Who scored the point? ";                 cin >> team;                 // increment the appropriate score                 switch (team) {         case 'n':         case 'N': nyu++;               break;         case 'u':         case 'U': usc++;               break;         default: cout <<"invalid entry"<<endl;                 cout << "Current Score: nyu: " << nyu                 << "  usc: " << usc << endl; }              // print final score     if (usc > nyu)                 cout << "\nBoo, usc: " << usc <<                 "  nyu: " << nyu;     else                 cout << "\nYeah, nyu: " << nyu <<                 "  usc: " << usc;     cout << "\n\n"; } }     system("PAUSE");     return EXIT_SUCCESS; }```
• 11-07-2010
Salem
I see I wasted my time on your other thread :mad:
http://cboard.cprogramming.com/cplus...hile-loop.html
• 11-07-2010
sergiosergio
yup, i reposted it and cleaned it up a little because i knew it was messy.
• 11-07-2010
whiteflags
>> yup, i reposted it and cleaned it up a little because i knew it was messy.
You could have bumped your own thread you know; you started a new one for a really lame reason.

Anyway, you get huge hints in the assignment:
Quote:

PART 1) Add the appropriate Boolean expression to the while. The while loop should exit when a team has scored at least 15 points and is ahead by at least 2. That is, the loop exits when the score is 15 – 8 but not 15 -14. Remember that the Boolean expression should be the opposite of the exit condition. Hint: A good strategy in this case is to write a Boolean expression for the stopping condition and apply the NOT operator, !, to it. (PART 2) Also program should play multiple games, wins match at 5 game wins with a difference of at least two games.
For part one all you have to do is answer some questions: What Boolean expression is true when a team scores at least 15 points? What Boolean expression is true when a team leads by at least 2? What Boolean operator will ensure that both of these are true when the loop is exiting? The hint explains how to use these answers in the actual condition.

Part two is a variation of the condition you figure out in one, but for a different loop. The loop whose body is the game code. Of course to play a game in a match, you have to win a game so the loops are not separate.

Does that explanation help?
• 11-08-2010
rags_to_riches
And cross-posted as well. Just looking for a spoon-feeding.
• 11-08-2010
Salem
Ah well, if it's cross-posted, then we can close it.