Program 5-4

Post date: Feb 11, 2013 9:09:18 AM

// This program calculates the number of soccer teams a youth

// league may create from the number of available players.

// Input validation is done with while loops.

#include <iostream>

using namespace std;

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

// Get the number of players per team

cout << "How many players do you wish per team?\n";

cout << "(Enter a value in the range 9 - 15): ";

cin >> teamPlayers;

// Validate the input

while ((teamPlayers) < 9) || (teamPlayers > 15))

{

cout << "Team size should be 9 to 15 players.\n";

cout << "How many players do you wish per team? ";

cin >> teamPlayers;

}

// Get the number of players available

cout << "How many players are available? ";

cin >> players;

// Validate the input

while (players <= 0)

{

cout << "Please enter a positive number: ";

cin >> players;

}

// Calculate the number of teams

numTeams = players / teamPlayers;

// Calculate the number of leftover players

leftOver = players % teamPlayers;

// Display the results

cout << "\nThere will be " << numTeams << " teams with ";

cout << leftOver << " players left over.\n";

return 0;

}

Program Output with ExamplExample Input Shown in Bold

How many players do you wish per team?

(Enter a value in the range 9 - 15): 4[Enter]

Team size should be 9 to 15 players.

How many players do you wish per team? 12[Enter]

How many players are available? –142[Enter]

Please enter a positive number: 142[Enter]

There will be 11 teams with 10 players left over.