While Loop Error

Why does this just keep looping?

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
     int shop_pick, item_type, amount;
     int inventory[11]={0};   // Non initialized arrays contain trash

     //for loops that will be used instead goto
     bool shop_loop = true;
     bool item_type_loop;
    
     while(shop_loop) {
          cout << "\n\n1)General Store          5)Bar";
          cout << "\n2)Blacksmith               6)Joe's House";
          cout << "\n3)Potions/Magic Shop";
          cout << "\n4)Weapons";
    
          switch(shop_pick){
               case 1: 
                    cout << "\n\nWelcome To The General store!" << endl;
                    cout << "1) 1 Gallon Water:"                << endl;
                    cout << "2) 1 lb Meat:"                     << endl;
                    cout << "3) 5 lbs Meat:"                    << endl;
                    cout << "4) Assortment of Spices/Herbs:"    << endl;
                    cout << "5) Wool Gloves:"                   << endl;
                    cout << "6) Wool Coat:"                     << endl;
                    cout << "7) Light Leather Boots:"           << endl;
                    cout << "8) Pack of 5 Torches:"             << endl;
                    cout << "9) Pack of 5 Lanterns:"            << endl;
                    cout << "10) 1 Quart of Oil:"       << endl << endl;    
               
                    item_type_loop = true;
                    while(item_type_loop){  
                         cout << "Press 1 - 10 and Then Enter To Purachase An Item. Press 'b' To Go Back To The \nShop List. When done, press 'd'";
                         cin  >> item_type;
                    
                         switch(item_type){
                              case 1: 
                              case 2:
                              case 3:
                              case 4:
                              case 5:
                              case 6:
                              case 7:
                              case 8:
                              case 9:
                              case 10:
                                   cout << "Enter amount: ";
                                   cin  >> amount;
                                   inventory[item_type]+=amount;
                                   cout << "Type " << item_type << " has " << inventory[item_type] << " items in it" << endl;
                                   break;
                              
                              case 'b':
                                   item_type_loop = false;
                                   break; 
                              
                              case 'd':
                                   item_type_loop = false;
                                   shop_loop = false;
                                   break; 
                              default:
                                   cout << "Incorrect Command!";     
                                   break;
                         }           
                      
                    }
          break;
          }
     }
    system("PAUSE");
    return EXIT_SUCCESS;
}
First thing that I noticed is that you only initialized the very first member of your inventory array. You might want to change your while loops into do(){} while(item_type_loop) or do(){}while(shop_loop). Also, if you press 'D', you won't break out, only if you press 'd'.
cin >> item_type; will ignore character inputs. so item_type will be 0 if the user enters d.
First error: where did you assign the value of shop_pick. If you dont set it then the compiler will give you some garbage value;

2nd error: check for error condition in shop_type. if a character is printed instead;

3rd error: change item_type to char instead of int. All your cases will be (char)10 or '1' in this manner.

4th error: This is a very messy program with too many switches and while. Clean up the program
I get that the program is messy and such, but most of the stuff you guys have said is irrelevant. There is just some stupid loop problem I'm facing, and I don't see what that problem is. I would like to understand that problem first, and then I will go back and clean it up.
@timmah1493:
but most of the stuff you guys have said is irrelevant.

it does not hurt to say what was not irrelevant.

next: why dont you say which of you two loops has the problem?
(not everybody has a compiler near by when reading here)

i thought you meant the second loop, because there i noticed the problem i mentioned and i had no compiler at that
point.

now i have one, just compiled your program and got this:
warning: ‘shop_pick’ may be used uninitialized in this function

you never set a value for shop_pick. not by hand, no cin...
Just like Jackel7777 told you

So many problems can be solved with reading the compiler warnings...
closed account (zb0S216C)
Mathes is correct. shop_pick hasn't been initialized. Always initialize variables before using them.
timmah1493 wrote:
I would like to understand that problem first, and then I will go back and clean it up.
The problem is that 'item_type' is an int. The input will never be a char like 'd' or 'b'. You can use temporarily a string for the input and convert it late to int for the 'item_type'
As mentioned the shop_pick is not initialized. Use cin to get user input for menu selection;
1
2
3
4
5
6
while(shop_loop) {
          cout << "\n\n1)General Store          5)Bar";
          cout << "\n2)Blacksmith               6)Joe's House";
          cout << "\n3)Potions/Magic Shop";
          cout << "\n4)Weapons";
		  cin >> shop_pick;
Yeah, stupid mistake, that worked. But, what if I don't want the user to click "enter" for the shop_pick? I've always used shop_pick = getch();, but it screws up now. I hit 1 and it just loops back to the beginning of shop_loop by printing out the stores again. Why?

I realize the problem with the char inside the switch now. How hard, or easy, is it to convert between chars and ints?

It's extremely easy to convert to an int.

1
2
3
char character;
int integer;
integer = int(character);


The way you have it written, the compiler might take care of it for you. 'd' has an int value of 100.

http://www.asciitable.com/

If I were you I would change item_type to a char instead. That way any character the user enters will be (closer to) valid.

1
2
3
4
5
6
7
8
9
char item_type;
switch(item_type)
{
case '1':
case '2':
// ...
case 'd':
// etc...
}
Last edited on
Topic archived. No new replies allowed.