@Jon15
---
To give you an example on why we need a constant 48 I will need to give you a scenario. Lets look at lines 117 through 168 (below will be that section):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
void BlueBox::rent(const unsigned short user)
{
unsigned short titleCounter = 0;
char answer;
while(true)
{
cout << "\t\t\tBlueBox Video Center\nWelcome " <<
m_users[user][0] << "!\n\nWhich Title to Rent:\n---\n";
for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
cout << ++titleCounter << ". " << setw(12) << m_inventory[titles][0] << " | " <<
m_inventory[titles][MAX_COPIES - 1] << " copies left\n";
if(titleCounter == 0)
error("NO DVDS TO RENT - OUT OF ORDER!\n");
cout << ++titleCounter << ". Exit\n---\nAnswer: ";
cin >> answer;
system("CLS");
if(answer - 48 == titleCounter)
break;
else if(answer - 48 > titleCounter || answer - 48 < 1)
{
titleCounter = 0;
continue;
}
else
{
unsigned short titlePickerCounter = 0;
for(unsigned short titles = 0; titles < MAX_TITLES; titles++)
{
if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
titlePickerCounter++;
if(answer - 48 == titlePickerCounter)
{
m_inventory[titles][MAX_COPIES - 1].at(0)--;
break;
}
}
}
titleCounter = 0;
}
}
| |
When the function is called, 3 Bytes are allocated for
titleCounter
and
answer
.
titleCounter
is used as a counter per say. In the for loop, if a title as 1 or more copies:
if(m_inventory[titles][MAX_COPIES - 1].at(0) > '0')
Then
titleCounter
gets incremented. So if we have 20 different titles and have 1 copy for each available title, then
titleCounter
will be 20. But, if we have 20 different titles and 4 of them has no copies available:
m_inventory[titles][MAX_COPIES - 1].at(0) == '0')
Then
titleCounter
will be 16. That being said, the titles displayed on the screen will be the ones with copies available for renting.
Lets say for example, 2 titles were displayed with the addition of exiting:
1. Jaws
2. The Grinch
3. Exit |
You will be prompted to pick a title. Lets say you want to pick 'The Grinch' which is '2'. You type '2' and then you press enter. Your answer will be stored in
answer
which is of type
char
. The reason we have
answer - 48
, is because '2' is really 50. Whenever you use
char
, your storing characters that are part of the ASCII. Every character is represented by a number (internally). In this case, '2' is represented by 50. So the constant 48 is used to properly validate the conditions in most of the code.
Hope I explained clearly.