////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;
}

Edited 3 Years Ago by pyTony: fixed formating

can u please post using code tags.

#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;
  }

it's working fine for me, what seems to be the problem exactly?

when the program runs it just scolls non stop at a fast speed with the stuff on the screen as if i were holding down the enter button.

uhh, do we have teh same version?? i compiled and ran it fine. i added items, printed them, listed them, selected individual ones, and the quit. strange :D

The problem is the input type is not being error proofed...if you enter intergers, it works fine; however, if you enter a string or character you get a flood (as described by you in your first post). There's an easy solution to this, make your array of type String and approprietly concatenate and format your strings. This way you can accept both ints, longs, and chars since the Strings class's + operator handles the conversion.

Oh almost forgot, in your program you tell the user to use Q to exit yet your code uses 'E' or 'e' , properly adjust either the code or menu :cool:

This article has been dead for over six months. Start a new discussion instead.