Thread: Help with "while loop"

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    4

    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;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    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;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I see I wasted my time on your other thread
    Help with while loop
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    4
    yup, i reposted it and cleaned it up a little because i knew it was messy.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> 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:
    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?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And cross-posted as well. Just looking for a spoon-feeding.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ah well, if it's cross-posted, then we can close it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-30-2010, 12:25 AM
  2. I don't understand "while (getchar() != '\n');"
    By valedor in forum C++ Programming
    Replies: 13
    Last Post: 09-08-2009, 05:12 PM
  3. "For Loop" Help
    By ggraz in forum C Programming
    Replies: 4
    Last Post: 11-11-2008, 07:30 AM
  4. How to make this "for loop"
    By kurko82 in forum C++ Programming
    Replies: 14
    Last Post: 09-23-2008, 10:03 AM