In line 29 to 33 the i is an index to the array gift, specifying which gift object current iteration of the loop will refer to.
In line 33 to 50 the i is an index to the array name, specifying which person current iteration of the loop will refer to. The i and currentperson are thus one and the same. Lets make it so and move the gift-printing loop inside the other loop too:
1 2 3 4 5 6 7 8 9 10
|
for ( int currentperson=0; currentperson < limit; ++currentperson )
{
cout << "The following gifts are available:\n";
for ( int gifidx=0; gifidx < limit; ++gifidx )
{
if ( available[gifidx] ) cout << gifidx+1 <<". "<< gift[gifidx] << '\n';
}
cout << "Enter the gift number for " << name[currentperson] << '\n';
// something
}
|
Descriptive(?) variable names might make the logic of the program easier to see.
1 2 3
|
index<0 || index>4
// is same as
index<0 or index>4
|
It is true, if:
* index is less than 0
OR
* index is more than 4
In other words, it is not true, if index is any of 0, 1, 2, 3, 4.
For the index to be valid, three conditions must be simultaneously true:
1. index is at least 0 ( 0 <= index)
AND
2. index is less than limit ( index < limit )
AND
3. that gift is still available ( available[index] )
There are two causes of invalid index:
1. index is not in range [0..limit)
2. gift is not available
They should give different error message.
In you original code you had:
1 2 3 4
|
while ( cin >> input )
{
// something
}
|
That
something should test for index validity and either end the loop (when index is valid) or give appropriate error message (and continue the loop). For example:
1 2 3 4 5 6 7 8 9
|
if ( /* out of range */ ) {
// tell that valid range is from 1 to limit
}
else if ( /* not available */ ) {
// tell that gift has already been given to somebody
}
else {
break;
}
|