943,503 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4636
  • C++ RSS
Mar 25th, 2004
2

Program runs in an infinite loop

Expand Post »
////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;
}
Similar Threads
Reputation Points: 14
Solved Threads: 0
Newbie Poster
popo_prince is offline Offline
12 posts
since Mar 2004
Mar 25th, 2004
0

Re: prog help

can u please post using code tags.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. const int MAX_ITEMS = 5;
  5.  
  6. typedef int InventoryArray[MAX_ITEMS];
  7.  
  8. void DisplayOpeningMessage();
  9. void DisplayEndMessage();
  10. void OutputInventory(const InventoryArray items); // const used to protect the array from being changed
  11. void InputInventory(InventoryArray items);
  12. void ProcessChoices();
  13.  
  14. void OutputOneInventoryAmount(int amount, int itemCode);
  15. void ChangeOneInventoryAmount(InventoryArray items, int itemCode);
  16. char GetItemCode();
  17.  
  18. void DeductInventory(InventoryArray items, int itemCode);
  19.  
  20.  
  21. void AddInventory(InventoryArray items, int itemCode);
  22.  
  23. char GetUsersChoice();
  24.  
  25.  
  26. void main()
  27. {
  28.  
  29.  
  30. DisplayOpeningMessage();
  31. ProcessChoices();
  32. DisplayEndMessage();
  33. }
  34.  
  35.  
  36.  
  37. void DisplayOpeningMessage()
  38.  
  39. {
  40. cout << "\n\n\n\n\n";
  41. cout << " Iventory\n\n";
  42. cout << " Program\n\n";
  43. cout << " Collective\n\n\n\n\n\n";
  44.  
  45. }
  46.  
  47. void DisplayEndMessage()
  48. { cout << " Thanks for your effort\n\n\n\n\n\n";
  49. }
  50.  
  51. void ProcessChoices()
  52. {
  53. InventoryArray itemInventory;
  54.  
  55. char choice;
  56. int itemCode;
  57.  
  58. InputInventory(itemInventory); // Create the initial inventory
  59.  
  60. bool done = false;
  61.  
  62. choice = GetUsersChoice();
  63. while (!done)
  64. {
  65. switch (choice)
  66. { case 'L': // List Inventory
  67. case 'l':
  68. OutputInventory(itemInventory);
  69. break;
  70.  
  71. case 'S': // Single product listed
  72. case 's':
  73. itemCode = GetItemCode();
  74. while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
  75.  
  76. { OutputOneInventoryAmount(itemInventory[itemCode], itemCode);
  77. itemCode = GetItemCode();
  78. cout << "\n\n\n";
  79. }
  80. break;
  81. case 'C':
  82. case 'c':
  83. itemCode = GetItemCode();
  84. while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
  85.  
  86. { ChangeOneInventoryAmount(itemInventory, itemCode);
  87. itemCode = GetItemCode();
  88. cout << "\n\n\n";
  89. }
  90. break;
  91. case 'A': // Add to inventory
  92. case 'a':
  93. itemCode = GetItemCode();
  94. while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
  95.  
  96. { AddInventory(itemInventory, itemCode);
  97. itemCode = GetItemCode();
  98. cout << "\n\n\n";
  99. }
  100. break;
  101. case 'D': // Deduct from inventory
  102. case 'd':
  103. itemCode = GetItemCode();
  104. while ((itemCode < MAX_ITEMS) && (itemCode >= 0))
  105.  
  106. { DeductInventory(itemInventory, itemCode);
  107. itemCode = GetItemCode();
  108. cout << "\n\n\n";
  109. }
  110. break;
  111. case 'E': //Exit from program
  112. case 'e':
  113. cout << "Do you want to really want top quit (Y) or (N)\n";
  114. char answ;
  115. cin >> answ;
  116. if ((answ =='Y') || (answ == 'y'))
  117. {
  118. done = true;
  119. }
  120.  
  121. break;
  122. default:
  123. cout << "Choice is invalid. Select another.\n\n";
  124. } // end switch
  125. if (!done)
  126. {
  127. choice = GetUsersChoice();
  128. }
  129.  
  130. } // end while
  131. }
  132.  
  133. char GetUsersChoice()
  134. { char choice;
  135. cout << "\n\n\n\n";
  136. cout << "Please make a selection:\n";
  137. cout << " <L> List inventory\n";
  138. cout << " <S> Show inventory of singular item\n";
  139. cout << " <D> Deduct singular item\n";
  140. cout << " <A> Add an item\n";
  141. cout << " <C> Change location of an item\n";
  142. cout << " <Q> Exit\n";
  143. cin >> choice;
  144. return choice;
  145. }
  146.  
  147.  
  148. char GetItemCode()
  149. { int code;
  150. cout << "Please enter the code for the item you wish to work with\n"; //+++++++++++++++trouble spot
  151. cout << "Enter a negative number to indicate you are finished\n";
  152. cin >> code;
  153. while (code > MAX_ITEMS -1) // check for code that is too high
  154. { cout << "Invalid item code. Please enter a valid code.\n";
  155. cin >> code;
  156. }
  157. // cin.ignore(1);
  158. return code;
  159.  
  160. }
  161.  
  162.  
  163. void OutputOneInventoryAmount(int amount, int itemCode)
  164. { cout << endl << endl << "The inventory amount for item " << itemCode
  165. << " is: " << amount << endl << endl;
  166. }
  167.  
  168. void OutputInventory(const InventoryArray items)
  169. {
  170. cout << "\n\n\n\n\n";
  171. for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
  172. {
  173. cout << "The inventory amount for item " << itemCode
  174. << " is: " << items[itemCode] << endl;
  175. }
  176. cout << "\n\n\n\n\n\n";
  177. }
  178.  
  179.  
  180. void InputInventory(InventoryArray items)
  181. { int itemAmount;
  182. for (int itemCode = 0; itemCode < MAX_ITEMS; itemCode++)
  183. {
  184. cout << "Please enter the inventory item " << itemCode << ": "; //++++++++++++++++trouble spot
  185. cin >> itemAmount;
  186. items[itemCode] = itemAmount;
  187. }
  188. }
  189.  
  190. void ChangeOneInventoryAmount(InventoryArray items, int itemCode)
  191. { int amount;
  192. cout << "Please enter the new amount in the inventory\n";
  193. cin >> amount;
  194. items[itemCode] = amount;
  195. }
  196.  
  197. void DeductInventory(InventoryArray items, int itemCode)
  198. { int amount;
  199. cout << "Please enter the amount sold of item\n";
  200. cin >> amount;
  201. items[itemCode] -= amount;
  202. }
  203.  
  204.  
  205. void AddInventory(InventoryArray items, int itemCode)
  206. { int amount;
  207. cout << "Please enter the amount to add of item\n";
  208. cin >> amount;
  209. items[itemCode] += amount;
  210. }
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 25th, 2004
0

Re: prog help

it's working fine for me, what seems to be the problem exactly?
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 25th, 2004
0

Re: prog help

Quote originally posted by infamous ...
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.
Reputation Points: 14
Solved Threads: 0
Newbie Poster
popo_prince is offline Offline
12 posts
since Mar 2004
Mar 25th, 2004
0

Re: prog help

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
Reputation Points: 47
Solved Threads: 2
Junior Poster in Training
infamous is offline Offline
77 posts
since Mar 2004
Mar 26th, 2004
0

Re: prog help

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
Reputation Points: 28
Solved Threads: 9
Posting Whiz in Training
BountyX is offline Offline
222 posts
since Mar 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I need help on my 'address book' assignment!
Next Thread in C++ Forum Timeline: Visual C++ and Excel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC