Help with functions,

So, I have worked on most of the functions in this assignment. Please tell me if it looks right and help me start int main().

A table tennis game is played such that the first player to 11 wins. However, the player must win by a least 2 points. So game scores of 11 to 0, 11 to 9, 12 to 10, and 21 to 19 are legitimate. Game scores of 4 to 1, 12 to 9, 8 to 5, and 10 to 8 are NOT legitimate. In addition, any negative scores should be immediately rejected. See the sample runs on exactly how to re-prompt the user for incorrect game scores.

The program is to print the winner of the round robin. The winner of the round robin is the person who either wins both their matches or if all three players have each won one match, the player with the better won-loss record. As one can see in Sample Run # 1 below, player A won 3
games and lost 4, player B won 4 games and lost 5, and player C won 5 games and lost 3. Player A has a winning percentage of (3.0 / (3 + 4)) which is about 42.9%, player B has a winning percentage of (4.0 / (4 + 5)) which is about 44.4%, and player C has a winning percentage of
(5.0 / (5 + 3)) which is 62.5%. So Player C is the winner of the round robin because Player C has the best winning percentage.

Lastly, note that there are scenarios in which there is NO clear winner solely based on number matches won and number of games won and lost. As in Sample Run # 2, each player won one match 3 wins to 0 losses and each finished the match with 3 games won and 3 games lost.

Here is my pseudocode:
A. Your main function should adhere to the following pseudocode:

Process one match of player A versus player B
Process one match of player B versus player C
Process one match of player C versus player A
Print the heading
Print the player results for player A
Print the player results for player B
Print the layer results for player C
if there is no clear winner then
write "There is no clear winner."
else
print winner


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
const int WSM = 11; //Short for WINNING_SCORE_MINIMUM
const int TWO = 2;
const int THREE = 3;
int main()
{
   string prompt1= "Enter first score: ";

   return 0;
}
//--------------------------------------------------------------
// This function reads in a table tennis score and returns the 
// value read if it is a legitimate score. A legitimate score
// is any nonnegative value. The function continues to read
// until a valid score is read issuing an error message if not 
// valid.
// params: OUT
//--------------------------------------------------------------
int ReadScore(string prompt)
{
   int scores; cout << prompt; cin >> scores;
   for( ;scores < 0; cin>> scores)
   {
      cout << endl << "Invalid score: " << scores << endl << prompt;
   }
   return scores;
}
//--------------------------------------------------------------
// This function returns true if the two table tennis scores
// entered are legitimate according to the present USATT rules 
// that stipulate the first player to 11 wins and must win by 2 
// points.
// It returns false otherwise.
// params: (IN,IN)
//--------------------------------------------------------------
bool LegitScores(int score1, int score2)
{
   if (score1 >= WSM || score2 >= WSM)
   {
      if ((score1 > WSM || score2 > WSM) && abs(score1 - score2) == TWO)
         return 0;
   }
   else if (score1 == WSM || score2 == WSM)
      return 0;
   else if ((score1 > WSM || score2 > WSM) && (abs(score1 - score2) == 1))
      return 1;
   return 1;
}
//--------------------------------------------------------------
// This function reads in table tennis scores for one table
// tennis game until legitimate scores are entered and returns 
// the points scored via the parameters, score1 and score2.
// params: INOUT, INOUT, IN
//--------------------------------------------------------------
void ProcessOneGame(int& score1, int& score2, string prompt)
{
   score1 = ReadScore(prompt);
   score2 = ReadScore(prompt);
   while (LegitScores(score1, score2) == false)
   {
      score1 = ReadScore(prompt);
      score2 = ReadScore(prompt);
   }
}
//--------------------------------------------------------------
// This function processes a table tennis match of best 3 of 5
// games to win a match.  Games are processed until the match is 
// won; the match is won when a player has won 3 games. Each
// players, wins and losses [player1GamesWon, player1GamesLoss, 
// player2GamesWon, player2GamesLoss] are updated appropriately.  
// Finally, once the winner of the match is determined, the
// appropriate player's match total is updated [player1Matches, 
// player2Matches].
// params: OUT,INOUT,INOUT, OUT, INOUT, INOUT
//--------------------------------------------------------------
void ProcessOneMatch(int& player1Matches, int& player1GamesWon,
   int& player1GamesLoss, int& player2Matches,
   int& player2GamesWon, int& player2GamesLoss)
{//all the following integer variables are temporary and abbreviated
   int w, x;
   int p1gw = player1GamesWon = 0;
   int p1gl = player1GamesLoss = 0;
   int p2gw = player2GamesWon = 0;
   int p2gl = player2GamesLoss = 0;
   while (player1GamesWon < THREE && player2GamesWon < 3)
   {
      ProcessOneGame(w, x);
      if (w < x)
      {
         player1GamesWon++;
         player2GamesLoss++;
      }
      else
      {
         player1GamesLoss++;
         player2GamesWon++;
      }
   }
   if (player1GamesWon == THREE)
      player1Matches++;
   else
      player2Matches++;
   player1GamesWon += p1gw;
   player1GamesLoss += p1gl;
   player2GamesWon += p2gw;
   player2GamesLoss += p2gl;
   return;
}
//--------------------------------------------------------------
// This function returns true if there is a tie (no clear 
// winner) based on the games won and matches won.  It returns 
// false otherwise.
// params: TODO
//--------------------------------------------------------------
bool NoClearWinner(int Aswins, int Asmatches,
   int Bswins, int Bsmatches,
   int Cswins)
{
   return false;
}
//--------------------------------------------------------------
// This function displays a players individual results
// params: TODO
//--------------------------------------------------------------
void PrintPlayerResults(char playerLetter, int matchesWon,
   int gamesWon, int gamesLost)
{
  
}
To call your functions, you have to put prototypes of your functions before main so that the compiler knows those functions exist.
For example, given int ReadScore(string prompt) { ... }
the prototype to put before main would be
int ReadScore(string prompt);

Then, it looks like you should be calling ProcessOneMatch from main (though that's just a guess). I personally don't see why they are references, but since ProcessOneMatch wants to take in references, you'll have to declare the variables you want to pass in main.
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
    int player1Matches = 0;
    int player1GamesWon = 0;
    int player1GamesLoss = 0;
    int player2Matches = 0;
    int player2GamesWon = 0;
    int player2GamesLoss = 0;
    ProcessOneMatch(player1Matches, player1GamesWon,
   player1GamesLoss, player2Matches,
   player2GamesWon, player2GamesLoss);
    
}


1
2
3
4
5
   int scores; cout << prompt; cin >> scores;
   for( ;scores < 0; cin>> scores)
   {
      cout << endl << "Invalid score: " << scores << endl << prompt;
   }

The logic here is a bit backwards; If the first input of scores is < 0, you'll be prompted again without any text to tell you what to do.
I would just change it to a while loop.
1
2
3
4
5
6
7
8
   int scores = -1;
   cout << prompt;
   cin >> scores;
   while (scores < 0)
   {
      cout << endl << "Invalid score: " << scores << endl << prompt;
      cin >> scores;
   }


1
2
const int TWO = 2;
const int THREE = 3;

That's rather funny in that it defeats the entire point of having named constants. I still don't know what the two and three actually represent. Perhaps "NUM_GAMES_TO_WIN"?
Last edited on
So I did all of these, and my program still won't compile. something about the amount of arguments for one of my functions
Don't need prototypes if you instead put main() at the end of the file. Online compiler at repl.it found a problem with your line ProcessOneGame(w, x); because it's missing the "prompt" parameter.

Tbh, it's a little strange to pass around some prompting text everywhere. Just have each of the functions do their own prompting. Since scores come in pairs (and you're always calling ReadScore twice in a row), I would change it to ReadScores, a function that prompts for two, and keeps going until *both* seem like valid input.

The comments on params could be done better. Try using Javadoc style, which a lot of scripts, like Doxygen, can turn into documentation manuals. For example, param[out] to mark out parameters. Just param or param[in] for regular parameters. Javadoc comments typically start with /** or /*! or three slashes, which some IDEs recognize and change the highlighting.

Javadoc explanation: https://en.wikipedia.org/wiki/Javadoc
Doxygen example for param from the official site: https://www.stack.nl/~dimitri/doxygen/manual/commands.html#cmdparam

1
2
3
4
5
6
7
8
/// This function reads in table tennis scores for one table
/// tennis game until legitimate scores are entered and returns 
/// the points scored via the parameters, score1 and score2.
///
/// @param[out] score1  First parsed score to write to
/// @param[out] score2  Second parsed score 
/// @param      prompt  Text shown to user before retrieving input
void ProcessOneGame(int& score1, int& score2, string prompt)


For non-void methods you can also describe what's returned with @returns

Last edited on
Topic archived. No new replies allowed.