| | |
Program runs in an infinite loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2004
Posts: 12
Reputation:
Solved Threads: 0
////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;
}
#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;
}
•
•
Join Date: Mar 2004
Posts: 77
Reputation:
Solved Threads: 2
can u please post using code tags.
C++ Syntax (Toggle Plain Text)
#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; }
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
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
A Hacker's Mind:
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes..." - J.D.Salinger
![]() |
Similar Threads
- Excel 2007 - infinite code loop (Windows Software)
- Methods for generating graphical displays (C++)
- First Post/Sudoku Solver problems (Java)
- Infinite Loop (C++)
- I have an infinite loop and I don't know why! (Python)
- where is my infinite loop coming from? (C++)
- How do you Terminate an Infinite Loop? Impossible!!! (C++)
- infinite loop (C++)
Other Threads in the C++ Forum
- Previous Thread: I need help on my 'address book' assignment!
- Next Thread: Visual C++ and Excel
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





