| mac193 (6) | |
|
This is currently my program. When I try running it, it gives me an error telling me that "The variable 'leftOver' if being used without being initialized". I'm not exactly sure how to fix this. TODO: Rewrite this program using the following 4 functions. (1) An input function for getting the number of players per team. (2) Another input function for getting the number of players available. (3) A function to calculate the number of teams and number of left-over players. (4) An output function to print the result. NOTE: Use and document the appropriate parameter passing mechanism for each parameter: in, out, inout In the end, the main() function should include just the variable declarations, 4 function call statements, and the return statement. */ #include <iostream> using namespace std; void getPlayers(int&); void playersPerTeam(int&); void numOfTeams(int&, int&, int&); void displayResults(int, int, int, int); int main() { int players, // Number of available players teamPlayers, // Number of desired players per team numTeams, // Number of teams leftOver; // Number of players left over getPlayers(players); playersPerTeam(teamPlayers); numOfTeams(numTeams, teamPlayers, players); displayResults(numTeams, leftOver, teamPlayers, numTeams); return 0; } void getPlayers(int& players) { cout << "How many players do you wish per team?\n"; cout << "(Enter a value in the range 9 - 15): "; cin >> players; // while loop. while (players <= 0) { cout << "Please enter a positive number: "; cin >> players; } } void playersPerTeam(int& teamPlayers) { // while loop. while (teamPlayers < 9 && teamPlayers > 15) { cout << "You should have at least 9 but no\n"; cout << "more than 15 per team.\n"; cout << "How many players do you wish per team? "; cin >> teamPlayers; } cout << endl; } void numOfTeams(int& numTeams, int& teamPlayers, int& players) { numTeams = players / teamPlayers; } void displayResults(int players, int leftOver, int teamPlayers, int numTeams) { leftOver = players % teamPlayers; cout << "There will be " << numTeams << " teams with "; cout << leftOver << " players left over.\n"; } /* Sample: How many players do you wish per team? (Enter a value in the range 9 - 15): 8 You should have at least 9 but no more than 15 per team. How many players do you wish per team? 16 You should have at least 9 but no more than 15 per team. How many players do you wish per team? 9 How many players are available? 0 Please enter a positive number: 50 There will be 5 teams with 5 players left over. */ | |
|
|
|
| tpdietz (4) | |
|
You don't necessarily need the leftOver int at all. In void displayResults, remove int leftOver from the formal (and actual) parameters and in body of your function simply enter players % teamPlayers where you currently have cout << leftOver. Example: void displayResults(int players, int teamPlayers, int numTeams) { cout << "There will be " << numTeams << " teams with "; cout << players % teamPlayers << " players left over.\n"; } | |
|
Last edited on
|
|
| mac193 (6) | |
|
Oh jeeze, I never even thought that! I think it will work now since I'm not getting an error message anymore. But, now I'm having a problem with my programming not asking me to enter an even number in.. | |
|
|
|
| tpdietz (4) | |
| I am a bit confused at what you mean. I'm not sure where the program would (or would not) prompt for an even number. | |
|
|
|
| mac193 (6) | |
|
Nevermind, I figured it out. Thank you so much for the help on my leftOver problem!! :) | |
|
|
|