How to end code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to end code?

So I am making a game where you have two choices. Left or right. When you go left you dies, right you live on and the story keeps on. I am using the int switch and stuff, but how do I do it so the code ends or restart when you go left?

1st Jun 2018, 9:08 AM
Mats Hvatum
Mats Hvatum - avatar
4 Answers
1st Jun 2018, 10:49 AM
‎ ‏‏‎Anonymous Guy
+ 3
#include <iostream> using namespace std; int main() { string name; cout << "Legend of the mushroom" << endl; cout << "First let me know your name" << endl; cout << "So what is your name ?" << endl; cin >> name; cout << "So your name is " << name << " ?" << endl; char gender; cout << "Your gender (b/g)" << endl; cout << "Type b for boy and g for girl" << endl; cin >> gender; if (gender == 'b') { cout << "So you are a boy?" << endl; } else { cout << "So you are a girl?" << endl; } cout << "You wander into the forest" << endl; cout << "You see two paths in front of you" << endl; int choice; cout << "Want to go Left or Right" << endl; cout << "Type 1 for Left or 2 for Right" << endl; cin >> choice; switch (choice) { case 1: cout << "You wander into the forest and see a big dark creature." << endl; cout << "It is a huge troll" << endl; cout << "It is wielding a wet towel" << endl; cout << "From the towel there are falling drops of acid" << endl; cout << "You died" << endl; break; // So rigth here i would want the code to end and restart case 2: cout << "You wander into the right" << endl; cout << "You find a portal" << endl; cout << "Should you go inside?" << "Why not, you thought" << endl; cout << "You walked into the portal, and what did you see?" << "A really nice hut in the maldives" << endl; cout << "You lived happily ever after" << endl; break; } cout << "You take a dive into the water" << endl;
1st Jun 2018, 9:09 AM
Mats Hvatum
Mats Hvatum - avatar
+ 2
INSTEAD OF SWITCH USE FOR LOOP. AFTER DECLARATION OF choice VARIABLE USE FOR LOOP AND INSTEAD OF SWITCH USE IF CHOICE = 1 (LEFT) DO THIS ELSE DO THIS. IF USER CHOOSE 1 PRINT STATEMENTS YOU WANT AND AT END USE continue STATEMENT WHICH LOOP OVER AGAIN AND ASK USER TO ENTER CHOICE. EVERY TIME USER CHOOSE 1 IT WILL PRINT STATEMENT AND ASK FOR CHOICE UNTILL FOR LOOP CONDITION HOLDS TRUE.
1st Jun 2018, 9:20 AM
Meet Mehta
Meet Mehta - avatar
+ 2
#include <iostream> using namespace std; int main(){ point924: //code if(choice == 1){ goto point924; }
1st Jun 2018, 9:33 AM
Shahil Ahmed
Shahil Ahmed - avatar