Go back to part of code
I am making a text adventure, I am a beginner at c++ and I need the code to loop back to a previous part of code every time you leave the shop.
Here is my code.
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
|
#include <iostream>
using namespace std;
int main()
{
string username;
cout << "Choose your username: ";
cin >> username; //User chooses Username
system("CLS"); //clears screen
cout << "Wizard: Hello " << username << "! Welcome to silkland!" << endl; //first dialogue
system("PAUSE");
system("CLS");
cout << "Feel free to look around the town!" << endl;
system("PAUSE");
system("CLS");
cout << "I'll be in my tower if you need me." << endl << "Goodbye! " << endl;
system("PAUSE");
system("CLS");
cout << "What would you like to do " << username << "? " << endl;
cout << "1 = shop, 2 = town hall" << endl;
int choice;
cin >> choice; //User picks place
if (choice == 1 ) { //if statement
cout << "You chose shop!" << endl;
system("PAUSE");
system("CLS");
int shop1
cout << "Shopkeeper: What would you like to buy?" << endl << endl;
cout << "1 = Iron Sword: 25 gold coins." << endl;
cout << "2 = Iron Shield: 40 gold coins." << endl;
cout << "3 = Iron 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 == 6) {
//Code here...
}
}
else if (choice == 2) {
cout << "You chose town hall!" << endl;
}
else {
cout << "Error 0: Invalid number. Shutting down..." << endl;
system ("PAUSE");
return 0;
}
system ("PAUSE");
return 0;
}
|
Use a while loop. Or use a for loop. Use some kind of loop.
If you need help with what these are, then go here:
http://www.cplusplus.com/doc/tutorial%20%20/control/
Look at Iteration Structure (loops)
Do not look at the "goto" statement. You should need to use it for this project.
Last edited on
Topic archived. No new replies allowed.