Loops 11

Description

The purpose of this challenge is to use the WHILE loop with other conditional statements (IF, IF-ELSE, etc). This exercise simulates the scoring of a tiebreak game in a tennis match.

Tiebreak Scoring in Tennis

  • Both players start at 0
  • Whoever wins a point gets a point added to their tally
  • Scores are typically displayed separated by a dash
  • The tiebreak will continue until one of the players gets a score of 7 or higher, while leading the other player with at least a difference of 2. Examples below:
    • 7-0 — This tiebreak is over. One player reached 7, the other got 0. The difference between their scores is 7
    • 7-5 — This tiebreak is over. One player reached 7, the other got 5. The difference between their scores is 2
    • 7-6 — This tiebreak must continue to be played. One player reached 7, the other got 6. The difference between their scores is only 1 (must be at least 2)
    • 9-9 — This tiebreak must continue to be played. One player reached 9, the other also got 9. The difference between their scores is 0 (must be at least 2)
    • 6-4 — This tiebreak is still in progress. Neither player has reached 7
  • After every 6 points, the players must switch ends on the tennis court. Examples below:
    • 3-3 — Total is 6. Players must switch ends
    • 2-4 — Total is 6. Players must switch ends
    • 6-6 — Total is 12. Players must switch ends.
    • 2-5 — Total is 7. Players stay on their current ends

Requirements

  1. Declare two strings: player1, player2
  2. Declare three integers: score1, score2 and input
  3. Declare a boolean variable: gameover. Initialize this to false.
  4. Ask the user to enter the name a tennis player. Receive this input into player1. Use the code below which allows you to enter a string that contains a space (such as a full name)
    cout << "Enter Player 1: ";
    getline(cin, player1);
    
  5. Do the same thing for player2
  6. To display the score, write the code below. The setw(xx) call is used for formatting text output. For example, if xx is 20, this displays the next piece of data aligned within 20 spaces. The length() property of a string will provide the number of characters in the string. For example if player1 contained “Serena” then player1.length() gives 6 (the number of characters in Serena)
     cout << player1 << setw(25 - player1.length()) << score1 << endl;
     cout << player2 << setw(25 - player2.length()) << score2 << endl;
    
  7. Add #include <iomanip> below the other #include at the top of the code
  8. Write a while loop block that will continue as long as gameover is not true. (Remember that this was initialized to false in Step 3). Inside the while loop, do the following:
    1. Ask the user “Who wins the next point (Player 1 or 2)?”. Use the input variable to receive the user’s response.
    2. Increment score1 if Player 1 wins the next point. Do the same for score2 accordingly.
    3. Display the score again by writing the same code from step 6
    4. Now the tricky part. Set gameover to true if either of Player 1 or Player 2  has reached a score of 7, and is ahead by at least 2 points.
    5. If the game is not over (gameover is not true) and the total number of  points played is divisible by 6, display a message “Players must switch ends before next point” This essentially covers the requirement of switching ends every six points.
  9. Display who wins (See sample interaction for how to format the final information)

DO NOT USE

You may not use any user-defined functions, or any other loops other than WHILE.

Sample Interaction / Output

Enter Player 1: Roger Federer
Enter player 2: Rafael Nadal

Roger Federer 0
Rafael Nadal  0

Who wins the next point (Player 1 or 2)? 1

Roger Federer 1
Rafael Nadal  0

Who wins the next point (Player 1 or 2)? 1

Roger Federer 2
Rafael Nadal  0

Who wins the next point (Player 1 or 2)? 2

Roger Federer 2
Rafael Nadal  1

Who wins the next point (Player 1 or 2)? 2

Roger Federer 2
Rafael Nadal  2

Who wins the next point (Player 1 or 2)? 2

Roger Federer 2
Rafael Nadal  3

Who wins the next point (Player 1 or 2)? 2

Roger Federer 2
Rafael Nadal  4

Players must switch ends before next point

Who wins the next point (Player 1 or 2)? 2

Roger Federer 2
Rafael Nadal  5

Who wins the next point (Player 1 or 2)? 1

Roger Federer 3
Rafael Nadal  5

Who wins the next point (Player 1 or 2)? 1

Roger Federer 4
Rafael Nadal  5

Who wins the next point (Player 1 or 2)? 1

Roger Federer 5
Rafael Nadal  5

Who wins the next point (Player 1 or 2)? 2

Roger Federer 5
Rafael Nadal  6

Who wins the next point (Player 1 or 2)? 2

Roger Federer 5
Rafael Nadal  7

Game over - Rafael Nadal wins 7-5

RUN IT AGAIN:

Enter Player 1: Carlos Alcaraz
Enter player 2: Frances Tiafoe

Carlos Alcaraz 0
Frances Tiafoe 0

Who wins the next point (Player 1 or 2)? 1

Carlos Alcaraz 1
Frances Tiafoe 0

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 1
Frances Tiafoe 1

Who wins the next point (Player 1 or 2)? 1

Carlos Alcaraz 2
Frances Tiafoe 1

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 2
Frances Tiafoe 2

Who wins the next point (Player 1 or 2)? 1

Carlos Alcaraz 3
Frances Tiafoe 2

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 3
Frances Tiafoe 3

Players must switch ends before next point

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 3
Frances Tiafoe 4

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 3
Frances Tiafoe 5

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 3
Frances Tiafoe 6

Who wins the next point (Player 1 or 2)? 2

Carlos Alcaraz 3
Frances Tiafoe 7

Game over - Frances Tiafoe wins 7-3

RUN IT AGAIN (Example of an extended tiebreak that goes past 7)

Enter Player 1: Leylah Fernandez
Enter player 2: Emma Raducanu

Leylah Fernandez 0
Emma Raducanu    0

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 1
Emma Raducanu    0

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 1
Emma Raducanu    1

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 2
Emma Raducanu    1

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 2
Emma Raducanu    2

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 3
Emma Raducanu    2

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 3
Emma Raducanu    3

Players must switch ends before next point

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 4
Emma Raducanu    3 

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 4
Emma Raducanu    4

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 5
Emma Raducanu    4

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 5
Emma Raducanu    5

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 6
Emma Raducanu    5

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 6
Emma Raducanu    6

Players must switch ends before next point

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 7
Emma Raducanu    6

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 7
Emma Raducanu    7

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 8
Emma Raducanu    7

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 8
Emma Raducanu    8

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 9
Emma Raducanu    8

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 9
Emma Raducanu    9

Players must switch ends before next point

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 10
Emma Raducanu    9

Who wins the next point (Player 1 or 2)? 2

Leylah Fernandez 10
Emma Raducanu    10

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 11
Emma Raducanu    10

Who wins the next point (Player 1 or 2)? 1

Leylah Fernandez 12
Emma Raducanu    10

Game over - Leylah Fernandez wins 12-10

LEGEND
PROGRAM OUTPUT
USER ENTRY
FROM INPUT

CATALOG ID: CPP-FLOW00011

Print Requirements