- We started with this code
cin >> playerAPot >> junk;
if (playerAPot > tokensPerPlayer and not error) {
error = true;
cout << "Error: player A contributed " << playerAPot
<< " tokens, which is more than the maximum " << tokensPerPlayer
<< "." << endl;
} else if (playerAPot < 0 and not error) {
error = true;
cout << "Error: player A contributed " << playerAPot
<< " tokens, which is negative." << endl;
}
cin >> playerBPot >> junk;
if (playerBPot > tokensPerPlayer and not error) {
error = true;
cout << "Error: player B contributed " << playerBPot
<< " tokens, which is more than the maximum " << tokensPerPlayer
<< "." << endl;
} else if (playerBPot < 0 and not error) {
error = true;
cout << "Error: player B contributed " << playerBPot
<< " tokens, which is negative." << endl;
}
cin >> playerCPot;
if (playerCPot > tokensPerPlayer and not error) {
error = true;
cout << "Error: player C contributed " << playerCPot
<< " tokens, which is more than the maximum " << tokensPerPlayer
<< "." << endl;
} else if (playerCPot < 0 and not error) {
error = true;
cout << "Error: player C contributed " << playerCPot
<< " tokens, which is negative." << endl;
}
- We discussed that people had errors due to cut-paste-edit but not change everything
- We decided that making a function would be a good idea.
- Focusing on the code we decided that
- The function should take two (later revised to three) parameters.
- The playerPot, an integer
- The playerName, a letter
- The maximum a player could bid, an int.
- We decided that none of these parameters would change, so they were all passed by value.
- We also decided that the function should return a bool
- True if there was an error
- False if there was not an error.
- We implemented the following function
bool PlayerValidation(int playerPot, char playerName, int max){
bool theFunctionError = false;
if (playerPot > max) {
theFunctionError = true;
cout << "Error: player " << playerName << " contributed " << playerPot
<< " tokens, which is more than the maximum " << max
<< "." << endl;
} else if (playerPot < 0) {
theFunctionError = true;
cout << "Error: player " << playerName << " contributed " << playerPot
<< " tokens, which is negative." << endl;
}
return theFunctionError;
}
- After this, the code above (in the main routine) became
if (not error) {
cin >> playerAPot >> junk;
error = PlayerValidation(playerAPot, 'A', tokensPerPlayer);
}
if (not error) {
cin >> playerBPot >> junk;
error = PlayerValidation(playerBPot, 'B', tokensPerPlayer);
}
if (not error) {
cin >> playerCPot;
error = PlayerValidation(playerCPot, 'C', tokensPerPlayer);
}