Serious Problem With Arrays

Here's the code.

using namespace std;

int main()
{

char b, d;
char item_type;
int s;
int amount;
char inventory[10];

loop15: // shop loop
cout << "\n\n1)General Store 5)Bar";
cout << "\n2)Blacksmith 6)Joe's House";
cout << "\n3)Potions/Magic Shop";
cout << "\n4)Weapons";
s = getch(); // goes into stores/back to menu of stores to purchase items
switch (s) {
case '1':
cout << "\n\nWelcome To The General store!\n";
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;
loop16:
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\n\n";
goto loop16;
}
break;
case 'b':
goto loop15;
break;
case 'd':
goto game;
break;
default:
{
cout << "Incorrect Command!";
goto loop16;
}
}

When It asks for an amount, I just put in a random number, and it will print "Item (1 - 10) has *SOME RANDOM SYMBOL* in it"

What the hell did I do??

Thanks,
Timmah \m/
Please use [code][/code] tags.
The line inventory[item_type]+=amount; adds an integer to an uninitialized character
... and remove those goto statements.
I don't know how to do this without goto statements - what's wrong with them?

And I changed inventory[10] to int rather than char at the beginning but then I run into a different problem:

case 1 and 3 work fine. 2 outputs a huge random number, and 4 - 10 crash the program...
The problem with goto is that if you're not very disciplined with yourself, you will write horrific, unstructured spaghetti code that is very difficult to follow and as such prone to bugs that are very hard to track down. Just like the code above.

What do you think should happen when you enter 2?

You've missed out some lines at the top, which make it harder to follow your code. I'm assuming you're including iostream and something to provide getch?

If you think your inventory items should all start with a value of zero, you need to set them to zero yourself when you create the inventory.
Last edited on
Here's the whole thing. How else could I do it rather than with goto commands. Also, shouldn't the array automatically set to 0 in the beginning?

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>

using namespace std;

int main()
{
    
    char b, d;
    char item_type;
    int s;
    int amount;
    int inventory[10]; 
    
    loop15:                                                                   // shop loop
    cout << "\n\n1)General Store          5)Bar";
    cout << "\n2)Blacksmith             6)Joe's House";
    cout << "\n3)Potions/Magic Shop";
    cout << "\n4)Weapons";
    s = getch();               // goes into stores/back to menu of stores to purchase items
      switch (s) {
             case '1':
                  cout << "\n\nWelcome To The General store!\n";
                  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; 
                  loop16:
                  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\n\n";
                              goto loop16;                        
                              }
                              break;
                         case 'b':
                              goto loop15;
                              break;
                         case 'd':
                              goto game;
                              break;
                         default:
                              {
                              cout << "Incorrect Command!";
                              goto loop16;
                              }         
                         }
                  
                  
             case '2':
                  cout << "\n\nWelcome To The Blacksmith!\n";
                  cout << "1) Hard Leather Gloves:" << endl; 
                  cout << "2) Hard Leather Boots:" << endl;
                  cout << "3) Hard Leather Torso:" << endl;
                  cout << "4) Chain Mail Helment:" << endl;
                  cout << "5) Chain Mail Torso:" << endl;
                  cout << "6) Chain Mail Pants:" << endl;
                  cout << "7) Plate Mail Helment:" << endl;
                  cout << "8) Plate Mail Torso:" << endl;
                  cout << "9) Plate Mail Pants:" << endl;
                  s = getch();
                    if (s=='b')
                    goto loop15;
                  break;
             case '3':
                  cout << "\n\nWelcome To The Potions and Magic Shop!\n"; 
                  cout << "1) Healing Potion:" << endl; 
                  cout << "2) Strength Potion:" << endl;
                  cout << "3) Poision:" << endl;
                  cout << "4) Dexterity Potion:" << endl;
                  cout << "5) Charisma Potion:" << endl;
                  cout << "6) Antidote Potion:" << endl;
                  cout << "7) Book of Fire:" << endl;
                  cout << "8) Book of Darkness:" << endl;
                  cout << "9) Book of Summons:" << endl;
                  cout << "10 Book of Healing:" << endl;
                  cout << "11) Mana Potion:" << endl;
                  cout << "12) Staff of Epicness:" << endl; 
                  s = getch();
                    if (s=='b')
                    goto loop15;
                  break;
             case '4':
                  cout << "\nweapons";
                  s = getch();
                    if (s=='b')
                    goto loop15;
                  break;
             case '5':
                  cout << "\nbar";
                  s = getch();
                    if (s=='b')
                    goto loop15;
                  break;
             case '6':
                  cout << "\njoe's house";
                  s = getch();
                    if (s=='b')
                    goto loop15;
                  break;
             default:
                  {
                  cout << "\n\nIncorrect Command!";
                  goto loop15;        
                  }
             }    
    
    game:
         cout << "GAME!";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Look at the code, explanations are inside.
It still looks ugly. I hope it helps.
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>

using namespace std;

int main(){
  char item_type;
  int s         ;
  int amount    ;
  //not initialized arrays contain trash
  int inventory[10]={0};

  //for loops that will be used instead goto
  bool loop15 = true;
  bool loop16;

  //needed later
  int int_item_type;

  while(loop15){                                                                 // shop loop

    cout << "\n\n1)General Store          5)Bar";
    cout << "\n2)Blacksmith             6)Joe's House";
    cout << "\n3)Potions/Magic Shop";
    cout << "\n4)Weapons";

    s = getch();               // goes into stores/back to menu of stores to purchase items

  switch (s) {
      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;

        loop16 = true;
        while(loop16){  //General store 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':
            // '10' is a string, since item_type is a char only 1 character will be kept ('1'), you can never get '10'
            // what's more inventory has a size of 10, that is from 0 to 9, so number of items with index 10 cannot be kept within it
            // solution use '0', letter or work on strings
            case '10':
              cout << "Enter amount: ";
              cin  >> amount;

              //Here was the problem with inventory
              //index to array is a integer value, item_type is a char, conversion is needed
              //in case of simple casting of char to int ascii valuse is given http://www.asciitable.com/
              //in case of numbers 0 to 9
              // number = ascii value of char - 48
              int_item_type = (int)item_type-48;
              //without this if you press '1' you won't get inventory[1] but inventory[49] -> trash
              inventory[int_item_type]+=amount;
              cout << "Type " << item_type << " has "<< inventory[int_item_type] << " items in it\n\n";

              //alternatively skip line int_item_type = (int)item_type-48; and put
              //inventory[(int)item_type-48;]+=amount;
              //cout << "Type " << item_type << " has "<< inventory[(int)item_type-48;] << " items in it\n\n";
              break;

            case 'b':
              loop16 = false;
              break;

            case 'd':
              loop15 = false;
              loop16 = false;
              break;

            default:
              cout << "Incorrect Command!";
              break;
          }
          //general store swich end 

        }
        // loop 16  end
      break;
      }
      // case item_type '1' end

      case '2':{
        cout << "\n\nWelcome To The Blacksmith!" << endl;
        cout << "1) Hard Leather Gloves:"        << endl;
        cout << "2) Hard Leather Boots:"         << endl;
        cout << "3) Hard Leather Torso:"         << endl;
        cout << "4) Chain Mail Helment:"         << endl;
        cout << "5) Chain Mail Torso:"           << endl;
        cout << "6) Chain Mail Pants:"           << endl;
        cout << "7) Plate Mail Helment:"         << endl;
        cout << "8) Plate Mail Torso:"           << endl;
        cout << "9) Plate Mail Pants:"           << endl;
        s=getch();
        break;
      }
      // case item_type '2' end

      case '3':{
        cout << "\n\nWelcome To The Potions and Magic Shop!\n";
        cout << " 1) Healing Potion:"            << endl;
        cout << " 2) Strength Potion:"           << endl;
        cout << " 3) Poision:"                   << endl;
        cout << " 4) Dexterity Potion:"          << endl;
        cout << " 5) Charisma Potion:"           << endl;
        cout << " 6) Antidote Potion:"           << endl;
        cout << " 7) Book of Fire:"              << endl;
        cout << " 8) Book of Darkness:"          << endl;
        cout << " 9) Book of Summons:"           << endl;
        cout << "10) Book of Healing:"           << endl;
        cout << "11) Mana Potion:"               << endl;
        cout << "12) Staff of Epicness:"         << endl;
      s=getch();
      break;
      }
      // case item_type '3' end

      case '4':
        cout << "\n\nweapons";
        s=getch();
        break;

      case '5':
        cout << "\n\nbar";
        s=getch();
        break;

      case '6':
        cout << "\n\njoe's house";
        s=getch();
        break;

      default:
        cout << "\n\nIncorrect Command!";

    }


  }

    cout << "GAME!";



    //no need for "system("PAUSE");" use
    getch();
    //instead
    return EXIT_SUCCESS;
}

Last edited on
If you use return 0; instead of return EXIT_SUCCESS; you won't need #include <cstdlib>

also #include <windows.h> is not necessary now, since we aren't using system("PAUSE");

I have a feeling you started with BASIC... no self-respecting programmer would write a book including how to use the "goto" keyword.
Topic archived. No new replies allowed.