The problem with gotois 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.
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <string>
#include <windows.h>
usingnamespace 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;
}