I'm interested in the organization, layout and style of how I program. How does organizing code using whitespace work? I've had a go in the code below. I mean like when you press tab and start the code further right into the page.
Was using the goto
command the best option for this code? Before learning C++, I learned batch, so I am used to using goto
and labels.
How do I organize this code better? What could I have done better in general?
I don't think there's any bugs in here, but I'd appreciate it if you could tell me if there are.
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
int choice;
int gold = 0;
int sword = 0;
int armour = 0;
int potion = 0;
int meat = 0;
int skin = 0;
system("cls");
cout << "\n Item added - 50 gold" << endl;
cout << "\n ";
system("pause");
start:
system("cls");
cout << "\n Where do you go?" << endl;
cout << "\n 1. Shop" << endl;
cout << "\n 2. Woods" << endl;
cout << "\n> ";
cin >> choice;
switch (choice) {
shop:
case 1:
system("cls");
cout << "\n Shop" << endl;
cout << "\n Buy or sell?" << endl;
cout << "\n 1. Buy" << endl;
cout << "\n 2. Sell" << endl;
cout << "\n 3. Go back" << endl;
cout << "\n> ";
cin >> choice;
switch (choice) {
buy:
case 1:
system("cls");
cout << "\n Shop, Buy" << endl;
cout << "\n What do you buy?" << endl;
cout << "\n 1. Sword 50 gold" << endl;
cout << "\n 2. Armour 100 gold" << endl;
cout << "\n 3. Potion 10 gold" << endl;
cout << "\n 4. Go back" << endl;
cout << "\n> ";
cin >> choice;
switch (choice) {
case 1:
if (gold < 50) {
system("cls");
cout << "\n You don't have enough gold!"
<< endl;
cout << "\n ";
system("pause");
goto buy;
} else {
system("cls");
gold = gold - 50;
sword = sword + 1;
cout << "\n Item added - Sword" << endl;
cout << "\n ";
system("pause");
goto buy;
}
case 2:
if (gold < 100) {
system("cls");
cout << "\n You don't have enough gold!"
<< endl;
cout << "\n ";
system("pause");
goto buy;
} else {
system("cls");
gold = gold - 100;
armour = armour + 1;
cout << "\n Item added - Armour" << endl;
cout << "\n ";
system("pause");
goto buy;
case 3:
if (gold < 10) {
system("cls");
cout << "\n You don't have enough gold!"
<< endl;
cout << "\n ";
system("pause");
goto buy;
} else {
system("cls");
gold = gold - 10;
potion = potion + 1;
cout << "\n Item added - Potion"
<< endl;
cout << "\n ";
system("pause");
goto buy;
case 4:
goto shop;
}
}
}
sell:
case 2:
system("cls");
cout << "\n Shop, Sell" << endl;
cout << "\n What do you sell?" << endl;
cout << "\n 1. Meat 5 gold Number: " << meat << endl;
cout << "\n 2. Skin 10 gold Number: " << skin << endl;
cout << "\n 3. Go back" << endl;
cout << "\n> ";
cin >> choice;
switch (choice) {
meat:
case 1:
if (meat < 1) {
system("cls");
cout << "\n You don't have any meat to sell!"
<< endl;
cout << "\n ";
system("pause");
goto sell;
} else {
system("cls");
gold = gold + 5;
meat = meat - 1;
cout << "\n Item added - 5 Gold" << endl;
cout << "\n ";
system("pause");
goto sell;
skin:
case 2:
if (skin < 1) {
system("cls");
cout << "\n You don't have any skin to "
"sell!" << endl;
cout << "\n ";
system("pause");
goto sell;
} else {
system("cls");
gold = gold + 10;
skin = skin - 1;
cout << "\n Item added - 10 Gold"
<< endl;
cout << "\n ";
system("pause");
goto sell;
}
}
}
case 3:
goto start;
}
}
}
goto
now (until you have a really, really good reason to used). By the way, it appear that you miss some}
or misplaced. \$\endgroup\$system("pause")
is like buying a new car because you want a key chain. \$\endgroup\$