I am having trouble making a menu system for a text based adventure game I am creating. I don't really understand all of the concepts yet but I am trying to go to a new situation by setting a number to be a certain situation and then by picking a certain action making the progress variable equal to that new situation but it doesn't seem to work and it merely loops around to the next (except for the first menu).

TLDR: My code doesn't work!

#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main ()
{
    int choice;
    int loop;
    loop = 1;
    
    while(loop==1)
    {
          system ("CLS");
          cout << "****Patrick's Awesome RPG!****\n\n"
               << "1. Start Game\n"
               << "2. Instructions(Read this first!)\n"
               << "3. Info\n"
               << "4. **EXIT**\n\n";
          cin >> choice;
          
          switch(choice)
          {
                  case 1:
                       loop = 2;
                       
                      
                  case 2:
                     
                 
                  case 3: 
                         
                 
                  case 4:
                        if(choice==4)
                        {
                           exit(0);
                        }

                   }
                 }


           while(loop==2)
           {
                system("CLS");
                cout << "You find yourself in complete darkness. You do not know where you are or how you got here. What do you do?\n\n";
                cout << "1. Start feeling around in order to figure out where you are\n";
                cout << "2. Start crying\n";
                cout << "3. Start running around blindly\n";
                cout << "4. Listen to your surroundings\n\n";
                cin >> choice;
                switch(choice)
                {
                              case 1:
                                   system ("CLS");
                                   cout << "      You slowly start to crawl and feel your way around in the inky blackness. the ground you are on feels like rough cobblstone and the place is damp and musty. You feel like you are in some sort of dungeon. Suddenly you feel something warm and hairy and it appears to be alive!\n";
                                   system ("PAUSE");
                                   loop = 3;
                              
                              case 2:
                                   system ("CLS");
                                   cout << "You start weeping loudly untill you hear a scratching sound and you are suddenly devoured!\n\n";
                                   system ("PAUSE");
                                   loop = 0;
                                   
                              
                              case 3:
                                   system ("CLS");
                                   cout << "You start running around in a panic untill you bump into something. That something swallows you whole\n\n";
                                   system ("PAUSE");
                                   loop = 0;
                              
                              case 4:
                                   cout << "You listen intently to the complete and utter silence untill you hear a slight rustling and snoring sound, something alive is with you!\n";
                                   system ("PAUSE");
                                   loop = 2;
                                   
                while(loop==0)
                {
                                   system ("CLS");
                                   cout << "You Are Dead\n";
                                   cout <<"GAME OVER\n\n";
                                   system ("PAUSE");
                                   loop = 1;
                }
                                   
                                   
                                   
                
                }
                }
}

You need a break; after the code in each of your case statements, else the control of the switch "falls through" to the next choice.

Changing the value of loop like makes your code difficult to follow, at least in my opinion. Get some more information on things like functions, etc. and your task will be somewhat easier.

3 points
1-use break after each switch case(as mentioned)
2-indent code properly and include comments (for your sanity)
3-while and switch alone cant make up a game. (they can make up mess though!)

3 more points
1-learn functions (as mentioned)
2-learn class, struct and data structures (at least stack and queue)
3-use data files to store messages as it is much better than splattering your code with strings (if possible)
Vinayak

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.