4 Replies - 743 Views - Last Post: 07 November 2010 - 06:38 PM Rate Topic: -----

#1 sergiosergio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-November 10

HELP WITH C++ WHILE LOOP

Posted 07 November 2010 - 04:04 PM

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)

#include <cstdlib>
#include <iostream>
using namespace std;
const int STOPSCORE=15;



int main(int argc, char *argv[])
{
	int nyu,GAMESPLAYED,gamecounter,game,game3,game4,game5, 
	    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";


}
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 07 November 2010 - 04:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: HELP WITH C++ WHILE LOOP

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: HELP WITH C++ WHILE LOOP

Posted 07 November 2010 - 04:06 PM

Welcome to DIC!

Please edit your posting (see the "EDIT" button in lower right). (If Edit is not available make a new posting on this thread and skip ( a ) below.)
( a ) Delete all your code.
( b ) Get a fresh copy of the code with formatting in place from your editor / IDE.
( c ) Paste the formatted code between code tags like this :code:
( d ) Use the "Preview Post" button to check it's all good.
( e ) Use the "Submit Modified Post" button to finish the editing.
Was This Post Helpful? 0
  • +
  • -

#3 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: HELP WITH C++ WHILE LOOP

Posted 07 November 2010 - 05:37 PM

Take a look at the logic for your second while loop. You are checking the values of usc and nyu. Variable scope is misleading you here. nyu and usc are scoped to main(), and will be in existence until the closure of your program. So after the first game is played, nyu or usc is already at 15 and your second loop will be skipped each iteration of the first while loop. I'll give you a hint at two methods of fixing this problem.

1. Temporary variables in place of usc and nyu, set equal to usc and nyu before the first game, that are initialized back to 10 after each game is played.

2. Initialize usc and nyu back to 10 after the second while loop.

I compiled your program and noticed you are not using most of those variables you declare (game, gamecounter). You should go ahead and remove those if you don't plan on doing something with them.
I also noticed that you are lacking a closing bracket for main(). You should go ahead and add that if you want your program to compile.

-alias
Was This Post Helpful? 1
  • +
  • -

#4 sergiosergio  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-November 10

Re: HELP WITH C++ WHILE LOOP

Posted 07 November 2010 - 06:31 PM

View Postalias120, on 07 November 2010 - 04:37 PM, said:

Take a look at the logic for your second while loop. You are checking the values of usc and nyu. Variable scope is misleading you here. nyu and usc are scoped to main(), and will be in existence until the closure of your program. So after the first game is played, nyu or usc is already at 15 and your second loop will be skipped each iteration of the first while loop. I'll give you a hint at two methods of fixing this problem.

1. Temporary variables in place of usc and nyu, set equal to usc and nyu before the first game, that are initialized back to 10 after each game is played.

2. Initialize usc and nyu back to 10 after the second while loop.

I compiled your program and noticed you are not using most of those variables you declare (game, gamecounter). You should go ahead and remove those if you don't plan on doing something with them.
I also noticed that you are lacking a closing bracket for main(). You should go ahead and add that if you want your program to compile.

-alias

Thanks for the reply alias. I understand what you are saying. It is allowing me to play 5 games with the corrections but can you give me an idea of how to have the program stop when a team wins by at least 2 games? Also the unnecessary variables such as game and gamecounter came about when i was trying various ways to make the program run. It will be deleted when the final version is done. Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: HELP WITH C++ WHILE LOOP

Posted 07 November 2010 - 06:38 PM

Similar to how you are keeping track of the scores of an individual game. Create counters for each team, if team1 wins increment their counter. If team2 wins increment their counter. Incorporate the check in your primary controlling loop alongside the check of number of games played.

-alias
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1