The errors in the program say all the cases besdies the 1st ones inside the "woods"
function are not within a switch statement, i think its something to do with the if statments i have inside the switches, any help to help me solve this would be great, thanks everyone. Everything works besides the woods function.

inside the woods function it should work like this,
it checks your levels and if its 1 you fight the young rat, 2 you fight the large rat, 3 the troll and 4 the demon, this is done with the switch statment. then inside the cases it used an if statment to check if you damanged it down to 0 or below in which case it lets you exit the if statement to the experience part that levels you up if your experience is past a certain limit. just thought i should explain to make it easier

//Wayne Daly

//Header files
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<iomanip>
#include<string>
using namespace std;


//global variables

string name;
int level;
int health;
int maxhealth;
int gold;
int attack;
int experience;


void tavern();
void blacksmith();
void village();
void woods();
void potionshop();
void village();
void stats();

void stats()
{
     cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     village();
}

     


void blacksmith()
{
cout<<"Welcome to blacksmith, go 2 village";
getch();
village();
}

void potionshop()
{
cout<<"Welcome to potionshop";
getch();
village();
}

void woods()
{
cout<<"Welcome to woods";
getch();

 if ( level == 1 ) 
 {       
 
 
 
 int youngrat_choice;
 int youngrat_health;
 int youngrat_maxhealth;
 
 youngrat_maxhealth = 10;
 youngrat_health = youngrat_maxhealth;
         
 cout<<"\nYou enter in battle with a young rat\n";
 
 
 startfightyoungrat:
 cout<<"It attacks you and deals you with 4 damage\n";
 health = (health - 4);
 getch();
 
 menuyoungrat:
 
 cout<<"\n1.Attack \n2.Enemy's Stats \n3.My Stats \n4. Excape";
 cin>>youngrat_choice;
 switch (youngrat_choice)
 
 case 1:
      cout<<"\nYou strike the young rat with your sword\n";
      youngrat_health = ( youngrat_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( youngrat_health <= 0 )
      {
           cout<<"You defeated the young rat and earned 5 experience\n points and 12 gold coins\n";
           experience = (experience + 5);
           gold = (gold + 12);
           getch();
           
      } 
      
      else if ( youngrat_health > 0 )
      {
           cout<<"The young rath is still alive!!";
           getch();
           goto startfightyoungrat;   
           
           }
           
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: Young Rat\nHealth: " << youngrat_health << "/" << youngrat_maxhealth << " ";
      getch();
      goto menuyoungrat;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menuyoungrat;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();   
      
 
 getch();
  }
  else if ( level == 2 )
 {       
 
 
 
 int largerat_choice;
 int largerat_health;
 int largerat_maxhealth;
 
 largerat_maxhealth = 25;
 largerat_health = largerat_maxhealth;
         
 cout<<"\nYou enter in battle with a large rat\n";
 
 
 startfightlargerat:
 cout<<"It attacks you and deals you with 10 damage\n";
 health = (health - 10);
 getch();
 
 menulargerat:
 
 cout<<"\n1.Attack \n2.Enemy's Stats \n3.My Stats \n4. Excape";
 cin>>largerat_choice;
 switch (largerat_choice)
 
 case 1:
      cout<<"\nYou strike the large rat with your sword\n";
      largerat_health = ( largerat_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( largerat_health <= 0 )
      {
           cout<<"You defeated the large rat and earned 5 experience\n points and 12 gold coins\n";
           experience = (experience + 5);
           gold = (gold + 12);
           getch();
           
      } 
      
      else if ( largerat_health > 0 )
      {
           cout<<"The large rat is still alive!!";
           getch();
           goto startfightlargerat;   
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: large Rat\nHealth: " << largerat_health << "/" << largerat_maxhealth << " ";
      getch();
      goto menulargerat;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menulargerat;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();   
      
 
 getch();
  }
   else if ( level == 3 )
 {       
 
 
 
 int troll_choice;
 int troll_health;
 int troll_maxhealth;
 
 troll_maxhealth = 50;
 troll_health = troll_maxhealth;
         
 cout<<"\nYou enter in battle with a troll\n";
 
 
 startfighttroll:
 cout<<"It attacks you and deals you with 35 damage\n";
 health = (health - 35);
 getch();
 
 menutroll:
 
 cout<<"\n1.Attack \n2.Enemy's Stats \n3.My Stats \n4. Excape";
 cin>>troll_choice;
 switch (troll_choice)
 
 case 1:
      cout<<"\nYou strike the troll with your sword\n";
      troll_health = ( troll_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( troll_health <= 0 )
      {
           cout<<"You defeated the troll and earned 25 experience\n points and 58 gold coins\n";
           experience = (experience + 25);
           gold = (gold + 58);
           getch();
           
      } 
      
      else if ( troll_health > 0 )
      {
           cout<<"The troll is still alive!!";
           getch();
           goto startfighttroll;   
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: troll\nHealth: " << troll_health << "/" << troll_maxhealth << " ";
      getch();
      goto menutroll;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menutroll;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();   
      
 
 getch();
  }
   else if ( level == 4 )
 {       
 
 
 
 int demon_choice;
 int demon_health;
 int demon_maxhealth;
 
 demon_maxhealth = 50;
 demon_health = demon_maxhealth;
         
 cout<<"\nYou enter in battle with a demon\n";
 
 
 startfightdemon:
 cout<<"It attacks you and deals you with 70 damage\n";
 health = (health - 70);
 getch();
 
 menudemon:
 
 cout<<"\n1.Attack \n2.Enemy's Stats \n3.My Stats \n4. Excape";
 cin>>demon_choice;
 switch (demon_choice)
 
 case 1:
      cout<<"\nYou strike the demon with your sword\n";
      demon_health = ( demon_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( demon_health <= 0 )
      {
           cout<<"You defeated the demon and earned 52 experience\n points and 108 gold coins\n";
           experience = (experience + 52);
           gold = (gold + 108);
           getch();
           
      } 
      
      else if ( demon_health > 0 )
      {
           cout<<"The demon is still alive!!";
           getch();
           goto startfightdemon;   
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: Demon\nHealth: " << demon_health << "/" << demon_maxhealth << " ";
      getch();
      goto menudemon;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menudemon;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();   
      
 
 getch();
  }
  else {
    cout<<"\nError\n";
    getch();
    village();
    
  }

//start of level up after battle phase
if ( experience >= 9 )
   {
   level = 1;
   }      
          

else if ( experience > 9 && experience < 25 )
        {
        level = 2;
        }
        
else if ( experience >24 && experience <=49 )
        {
        level = 3;
        }
        
else if  ( experience >= 50 )
         {
         level = 4;
         }
         
         
                                                               
        




getch();
village();
}










void tavern()
{
  int choice_tavern;   
     
cout<<"\nWelcome to the local Tavern";
cout<<"\n1. Room for the night\n2. Any news?\n3. Exit\n\n";     
cin>>choice_tavern;

switch (choice_tavern)
{
case 1:   cout << "You wake up the following day feeling\n";
          cout << "fully refreshed * Health is now full *";
          health = maxhealth;
          
          getch();
          break;

case 2:  cout << "Hmm the evil forces from the woods seem to be more aggressive\n";
         getch();
         break;
         
case 3:  cout << "Goodbye!\n";
         getch();
         break;
         
default: cout<< "Sorry I dont understand you!";
         getch();
         break;

}
     
village();    
     
}     






 void village()
 {
 
 
 int village_choice;
 
 
 //start village
 cout<<"\n\nYou enter the village";
 cout<<"\n1. Tavern\n2. Blacksmith\n3. Potion Shop\n4. Woods\n5. Stats \n\n";
 cin>>village_choice;

 
 
 switch(village_choice) 
 
 {
case 1:  tavern();

case 2:  blacksmith();
         
case 3:  potionshop();

case 4:  woods();

case 5:  stats();
         
default: cout<< "Sorry I dont understand you!";
         getch();
         village();
}
     

 
}   





int main()
{
    
 health = 100;
 level = 1;
 maxhealth = 100;
 gold = 0;
 attack = 2;
 experience = 1;
 
    
 cout<<"Enter your name warrior!\n";
 cin>>name;   
 village();
 }

Edited 5 Years Ago by zendet: n/a

Actually, the problem is that you are not enclosing the switch() block in braces, and as a result, only the first case is being seen as in the scope of the switch() statement. This is no different than if you had written an if statement like this:

if (something)
    statement1;
    statement2;
    statement3;

Despite the appearances due to the indentation, only statement1 is actually part of the if() statement. Thus, you actually want it to be like this:

switch (youngrat_choice)
 {
 case 1:
      cout<<"\nYou strike the young rat with your sword\n";
      youngrat_health = ( youngrat_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( youngrat_health <= 0 )
      {
           cout<<"You defeated the young rat and earned 5 experience\n points and 12 gold coins\n";
           experience = (experience + 5);
           gold = (gold + 12);
           getch();
           
      } 
      
      else if ( youngrat_health > 0 )
      {
           cout<<"The young rath is still alive!!";
           getch();
           goto startfightyoungrat;   
           
           }
           
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: Young Rat\nHealth: " << youngrat_health << "/" << youngrat_maxhealth << " ";
      getch();
      goto menuyoungrat;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menuyoungrat;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();   
      
 }
 getch();
  }
  else if ( level == 2 )
 {       
 
 
 
 int largerat_choice;
 int largerat_health;
 int largerat_maxhealth;
 
 largerat_maxhealth = 25;
 largerat_health = largerat_maxhealth;
         
 cout<<"\nYou enter in battle with a large rat\n";
 
 
 startfightlargerat:
 cout<<"It attacks you and deals you with 10 damage\n";
 health = (health - 10);
 getch();
 
 menulargerat:
 
 cout<<"\n1.Attack \n2.Enemy's Stats \n3.My Stats \n4. Excape";
 cin>>largerat_choice;
 switch (largerat_choice)
 {
 case 1:
      cout<<"\nYou strike the large rat with your sword\n";
      largerat_health = ( largerat_health - attack );
      cout<<"You deal the enemy " << attack << " damage\n";
      
      if ( largerat_health <= 0 )
      {
           cout<<"You defeated the large rat and earned 5 experience\n points and 12 gold coins\n";
           experience = (experience + 5);
           gold = (gold + 12);
           getch();
           
      } 
      
      else if ( largerat_health > 0 )
      {
           cout<<"The large rat is still alive!!";
           getch();
           goto startfightlargerat;   
      
 case 2:
      cout<<"\nEnemys Stats: \n\nType: large Rat\nHealth: " << largerat_health << "/" << largerat_maxhealth << " ";
      getch();
      goto menulargerat;
      
 case 3:          
         cout<<"Name: " << name << " ";
     cout<<"\nLevel: " << level << " ";
     cout<<"\nGold: " << gold << " ";
     cout<<"\nAttack: " << attack << " ";
     cout<<"\nExperience: " << experience << " ";
     cout<<"\nHealth: " << health << "/" << maxhealth << " ";
     
     getch();
     goto menulargerat;
     
     
  case 4:
       cout<<"\nYou run away in panic after being attacked\n";
       getch();
       village();
  }

Now, I'm sure you're going to get complaints about the excessive use of goto from others, so I won't bore you with that; suffice it to say, you could easily find ways to re-write the code so as to avoid the goto s, making it much easier to read and maintain. Similarly, you'll hear plenty about using getch() soon enough, so I'll refrain from taking you to task for it myself. Rather, I shall point out that coding the individual opponents manually is itself rather cumbersome, and you may want to work out a more general approach, one involving creating a general Monster class and sub-classing it for the specific types perhaps.

Edited 5 Years Ago by Schol-R-LEA: n/a

Thank you the mistake was so simple yet I would have never worked it out without your help, yes i know using gotos are wrong but I just could not think of a way to get back to where i wanted without them in certain areas like getting to the middle of a function so as to not repeat the welcome part at start.

What would i use in the place of getch(); as i heard cin.ignore(); would not be good?

I will look up classes as I have not learnt them yet, I am just in my first week of a programming course, so just trying to get used to switches and functions etc, thanks!

Use cin.get() or cin.getline(). Both are C++ input, rather than non-standard C input.

And get rid each and every goto statement. They are not needed if you program your case statements correctly and add appropriate loops your program.

This article has been dead for over six months. Start a new discussion instead.