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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    SourceForge.net: Indentation - cpwiki
    Try indenting your code.

    The compiler doesn't give a hoot about the lack of indentation, or the random newlines thrown in for good measure.

    > int nyu,GAMESPLAYED,gamecounter,game,game3,g
    > usc;
    WTF are those dots!?

    Line continuation characters?

    Did you type this on your phone or something.
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM