Text Adventure Bug
Aug 10, 2012 at 10:57am Aug 10, 2012 at 10:57am UTC
I am making a Text Adventure, and there's an annoying bug that I've come across.
Read this code:
1 2 3 4 5 6 7 8 9 10 11 12 13
SHOP1:{}
int shop1 = 0;
cout << "Shopkeeper: What would you like to buy?" << endl << endl;
cout << "1 = Bronze Sword: 25 gold coins." << endl;
cout << "2 = Bronze Shield: 40 gold coins." << endl;
cout << "3 = Bronze Armour: 75 gold coins." << endl;
cout << "4 = Apple: 5 gold coins. " << endl;
cout << "5 = Steak: 15 gold coins." << endl;
cout << "6 = Leave Shop." << endl << endl; //leave shop choice
cin >> shop1;
if (shop1 = 1){if (coins > 24){cout << "Thank you!\n" ; system("PAUSE" ); system("CLS" ); goto SHOP1;} else {cout << "Sorry, you haven't got enough coins.\n" ; system("PAUSE" ); system("CLS" ); goto SHOP1;}}
else if (shop1 = 6){goto MENU;}
When I press 1 it says Sorry, you haven't got enough coins.
This is meant to happen, but when I press 6, it says the same. How can I fix this?
Aug 10, 2012 at 11:21am Aug 10, 2012 at 11:21am UTC
Don't put everything on one line. It makes it hard to read.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
if (shop1 = 1)
{
if (coins > 24)
{
cout << "Thank you!\n" ;
system("PAUSE" );
system("CLS" );
goto SHOP1;
}
else
{
cout << "Sorry, you haven't got enough coins.\n" ;
system("PAUSE" );
system("CLS" );
goto SHOP1;
}
}
else if (shop1 = 6)
{
goto MENU;
}
The problem is that you use = instead of ==.
Aug 10, 2012 at 11:33am Aug 10, 2012 at 11:33am UTC
Using goto is a very bad habit that you should kill now. C++ provides three standard loop constructs; while , do-while and for . You can also write functions.
Aug 10, 2012 at 11:36am Aug 10, 2012 at 11:36am UTC
sing goto is a very bad habit that you should kill now. C++ provides three standard loop constructs; while, do-while and for. You can also write functions.
I tried using them, but they weren't working.
The problem is that you use = instead of ==.
Thanks! You are a life saver!
Aug 10, 2012 at 11:46am Aug 10, 2012 at 11:46am UTC
I tried using them, but they weren't working.
I expect you did it incorrectly. You should learn how to use them now while you're making very simple programs. If you wait until the point that your programs collapse under the weight of their goto-inspired spaghetti-coded mess, it will be much harder.
Topic archived. No new replies allowed.