////having trouble with this one after first input starts a crazy loop malf.
#include <iostream.h>
#include <conio.h>
const int MAX_ITEMS = 5;
typedef int InventoryArray[MAX_ITEMS];
void DisplayOpeningMessage();
void DisplayEndMessage();
void OutputInventory(const InventoryArray items); // const used to protect the array from being changed
void InputInventory(InventoryArray items);
void ProcessChoices();
void OutputOneInventoryAmount(int amount, int itemCode);
void ChangeOneInventoryAmount(InventoryArray items, int itemCode);
char GetItemCode();
void DeductInventory(InventoryArray items, int itemCode);
void AddInventory(InventoryArray items, int itemCode);
char GetUsersChoice();
void main()
{
DisplayOpeningMessage();
ProcessChoices();
DisplayEndMessage();
}
void DisplayOpeningMessage()
{
cout << "\n\n\n\n\n";
cout << " Iventory\n\n";
cout << " Program\n\n";
cout << " Collective\n\n\n\n\n\n";
}
void DisplayEndMessage()
{ cout << " Thanks for your effort\n\n\n\n\n\n";
}
void ProcessChoices()
{
InventoryArray itemInventory;
char choice;
int itemCode;
InputInventory(itemInventory); // Create the initial inventory
bool done = false;
choice = GetUsersChoice();
while (!done)
{
switch (choice)
{ case 'L': // List Inventory
case 'l':
OutputInventory(itemInventory);
break;
case 'S': // Single product listed
case 's':
itemCode = GetItemCode();
while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
{ OutputOneInventoryAmount(itemInventory[itemCode], itemCode);
itemCode = GetItemCode();
cout << "\n\n\n";
}
break;
case 'C':
case 'c':
itemCode = GetItemCode();
while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
{ ChangeOneInventoryAmount(itemInventory, itemCode);
itemCode = GetItemCode();
cout << "\n\n\n";
}
break;
case 'A': // Add to inventory
case 'a':
itemCode = GetItemCode();
while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
{ AddInventory(itemInventory, itemCode);
itemCode = GetItemCode();
cout << "\n\n\n";
}
break;
case 'D': // Deduct from inventory
case 'd':
itemCode = GetItemCode();
while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
{ DeductInventory(itemInventory, itemCode);
itemCode = GetItemCode();
cout << "\n\n\n";
}
break;
case 'E': //Exit from program
case 'e':
cout << "Do you want to really want top quit (Y) or (N)\n";
char answ;
cin >> answ;
if ((answ =='Y') || (answ == 'y'))
{
done = true;
}
break;
default:
cout << "Choice is invalid. Select another.\n\n";
} // end switch
if (!done)
{
choice = GetUsersChoice();
}
} // end while
}
char GetUsersChoice()
{ char choice;
cout << "\n\n\n\n";
cout << "Please make a selection:\n";
cout << " <L> List inventory\n";
cout << " <S> Show inventory of singular item\n";
cout << " <D> Deduct singular item\n";
cout << " <A> Add an item\n";
cout << " <C> Change location of an item\n";
cout << " <Q> Exit\n";
cin >> choice;
return choice;
}
char GetItemCode()
{ int code;
cout << "Please enter the code for the item you wish to work with\n"; //+++++++++++++++trouble spot
cout << "Enter a negative number to indicate you are finished\n";
cin >> code;
while (code > MAX_ITEMS -1) // check for code that is too high
{ cout << "Invalid item code. Please enter a valid code.\n";
cin >> code;
}
// cin.ignore(1);
return code;
}
void OutputOneInventoryAmount(int amount, int itemCode)
{ cout << endl << endl << "The inventory amount for item " << itemCode
<< " is: " << amount << endl << endl;
}
void OutputInventory(const InventoryArray items)
{
cout << "\n\n\n\n\n";
for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
{
cout << "The inventory amount for item " << itemCode
<< " is: " << items[itemCode] << endl;
}
cout << "\n\n\n\n\n\n";
}
void InputInventory(InventoryArray items)
{ int itemAmount;
for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
{
cout << "Please enter the inventory item " << itemCode << ": "; //++++++++++++++++trouble spot
cin >> itemAmount;
items[itemCode] = itemAmount;
}
}
void ChangeOneInventoryAmount(InventoryArray items, int itemCode)
{ int amount;
cout << "Please enter the new amount in the inventory\n";
cin >> amount;
items[itemCode] = amount;
}
void DeductInventory(InventoryArray items, int itemCode)
{ int amount;
cout << "Please enter the amount sold of item\n";
cin >> amount;
items[itemCode] -= amount;
}
void AddInventory(InventoryArray items, int itemCode)
{ int amount;
cout << "Please enter the amount to add of item\n";
cin >> amount;
items[itemCode] += amount;
}