Chapter 5

Program 5-4

posted Feb 11, 2013, 1:09 AM by Karwan Jacksi   [ updated Feb 11, 2013, 1:09 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.

Postfix and Prefix modes of the increment and decrement operators

posted Feb 11, 2013, 12:57 AM by Karwan Jacksi   [ updated Feb 11, 2013, 12:58 AM ]

// This program demonstrates the postfix and prefix
// modes of the increment and decrement operators.

#include <iostream>
using namespace std;

int main()
{
int num = 4;

// Illustrate postfix and prefix ++ operator
cout << num << " "; // Displays 4
cout << num++ << " "; // Displays 4, then adds 1 to num
cout << num << " "; // Displays 5
cout << ++num << "\n\n"; // Adds 1 to num, then displays 6

// Illustrate postfix and prefix -- operator
cout << num << " "; // Displays 6
cout << num-- << " "; // Displays 6, then subtracts 1 from num
cout << num << " "; // Displays 5
cout << --num << "\n\n"; // Subtracts 1 from num, then displays 4

return 0;
}

Program Output
4 4 5 6
6 6 5 4

The ++ and -- operators.

posted Feb 11, 2013, 12:53 AM by Karwan Jacksi   [ updated Feb 11, 2013, 12:54 AM ]

// This program demonstrates the ++ and -- operators.

#include <iostream>
using namespace std;

int main()
{
int num = 4; // num starts out with 4

// Display the value in num
cout << "The variable num is " << num << endl;
cout << "I will now increment num.\n\n";
// Use postfix ++ to increment num
num++;
cout << "Now the variable num is " << num << endl;
cout << "I will increment num again.\n\n";

// Use prefix ++ to increment num
++num;
cout << "Now the variable num is " << num << endl;
cout << "I will now decrement num.\n\n";

// Use postfix -- to decrement num
num--;
cout << "Now the variable num is " << num << endl;
cout << "I will decrement num again.\n\n";

// Use prefix -- to increment num
--num;
cout << "Now the variable num is " << num << endl;
return 0;
}

Program Output
The variable num is 4
I will now increment num.

Now the variable num is 5
I will increment num again.

Now the variable num is 6
I will now decrement num.

Now the variable num is 5
I will decrement num again.

Now the variable num is 4

1-3 of 3