Jump to content
  • Advertisement
Sign in to follow this  
Kenny77

Switch statement problems

This topic is 4480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having problems with the switch statement, whenever I compile this code I get an error that says: "in function int main" "jump to case label"
[source lang"cpp]
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{
    srand(time(0));
    int userHealth = 8, cpuHealth = 8;
    int userJab = rand() % 2, cpuPunch = rand() % 3;
    int userHay = rand() % 4;
    int userChoice;
    char opponent;
    string opponents;
    
    cout << "Choose your opponent.\n";
    cout << "a)Scott Stevens.\n";
    cout << "b)Tie Domi.\n";
    cout << "c)Darcy Tucker.\n";
    cout << "d)Donald Brasheer.\n";
    cout << "e)Todd Bertuzzi.\n";
    cout << "f)Zdeno Chara.\n";
    cout << "Opponent: ";
    cin >> opponent;
    
    switch (opponent)
    {
           case 'a' : cout << "You decide to take Scott Stevens on.\n"; 
                string opponents = "Stevens"; break;
           case 'b' : cout << "You decide to take Tie Domi on.\n"; 
                string opponents = "Domi"; break;
           case 'c' : cout << "You decide to take Darcy Tucker on.\n"; 
                string opponents = "Tucker"; break;
           case 'd' : cout << "You decide to take Donald Brasheer on.\n"; 
                string opponents = "Brasheer"; break;       
           case 'e' : cout << "You decide to take Todd Bertuzzi on.\n"; 
                string opponents = "Bertuzzi"; break;
           case 'f' : cout << "You decide to take Zdeno Chara on.\n"; 
                string opponents = "Chara"; break;
           default : cout << "Error.\n";
    }
    
    cout << "You and " << opponents << " come togethor,\n";
    while (userHealth || cpuHealth > 0)
    {
          if (cpuPunch == 0)
          {
             cout << opponents << " lands a shot.\n";
             --userHealth;
          }
          else cout << opponents << " swings but misses.\n";
          cin.get();
          cout << "Choose your punch type:\n";
          cout << "1)Haymaker\n";
          cout << "2)Jab\n";
          cout << "Choice: ";
          cin >> userChoice;
          cin.get();
          if (userChoice == 1)
          {
                         
              if (userHay == 0)
              {
                 cout << "You land a shot.\n";
                 cpuHealth = cpuHealth - 2;
              }
              else cout << "You swing and miss.\n";
          }
          else cout << "You swing but miss.\n";
          
          else if (userChoice == 2)
          {
               if (userJab == 0)
               {
                  cout << "You land a shot.\n";
                  cpuHealth = cpuHealth - 2;
               }
               else cout << "You swing and miss.\n";
          }
    }
    if (userHealth < 1)
    {
       cout << "You lost the fight to " << opponents << ".\n";
       cout << "You leave with a broken nose and bruised ego.\n\n";
       system ("pause");
       return 0;
    }
    else if (cpuHealth < 1)
    {
         cout << "You land the final blow and send " << opponents << " facedown on the ice.\n\n";
         system ("pause");
         return 0;
    }

Share this post


Link to post
Share on other sites
Advertisement
You can't declare a variable inside a switch statement like that I'm afraid. Declare opponents before the switch statement then just assign it after each case.


string opponents;

switch (opponent)
{
case 'a' : cout << "You decide to take Scott Stevens on.\n";
opponents = "Stevens"; break;
case 'b' : cout << "You decide to take Tie Domi on.\n";
opponents = "Domi"; break;
case 'c' : cout << "You decide to take Darcy Tucker on.\n";
opponents = "Tucker"; break;
case 'd' : cout << "You decide to take Donald Brasheer on.\n";
opponents = "Brasheer"; break;
case 'e' : cout << "You decide to take Todd Bertuzzi on.\n";
opponents = "Bertuzzi"; break;
case 'f' : cout << "You decide to take Zdeno Chara on.\n";
opponents = "Chara"; break;
default : cout << "Error.\n";
}



You can declare a variable in a case statement if you do this:

case 'a': { int a=10; }

but then the scope of the variable is limited to the enclosing braces so this would not be of use to you in this case.

HTH Paul

Share this post


Link to post
Share on other sites
Also this is not advisable:

system ("pause");
return 0;


A better function to use would be this one I use:

#include <string>

....

string Exit;
getline (cin, Exit);
return 0;

Share this post


Link to post
Share on other sites
Or, assuming pretty much any Windows compiler:

#include <conio.h>

void pause_me()
{
getch();
}

will actually physically wait for a keypress rather than require you to press return.

Share this post


Link to post
Share on other sites
0) DON'T ARTIFICIALLY PAUSE YOUR PROGRAM AT THE END. Learn to run it properly, i.e. from a console window or batch script. This is FAQ material that gets discussed around here all the time.

1) It is not "jump to case label", but rather something like "jump to case label skips initialization". This occurs because of what EasilyConfused pointed out.

Don't try to reproduce error messages from memory; copy and paste them to your post. Include the WHOLE error message, and the line numbers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!