-4

this is my first post. Im an amateur programmer who just recently learned Classes and Inheritance. So I thought I'd practice what I've learned by attempting to make a text based RPG. However, im having a problem with my Switch statement in this function:

...
void Character::classSelect()
{
    cout << "---- Choose the Class of your Hero ----\n";
    cout << "---- (1) -- Warrior -------------------\n";
    cout << "---- (2) -- Mage ----------------------\n";
    cout << "---- (3) -- Battlemage ----------------\n";
    cout << "---------------------------------------\n";
    int c;
    cin >> c;
    switch (c)
    {
    case 1:  m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
    case 2:  m_class = "Mage", hp = 20, physDmg = 1, magicDmg = 10;
    case 3:  m_class = "Battlemage", hp = 25, physDmg = 7, magicDmg = 7;  
    default: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
        cout << "You chose : " << m_class << endl;
    }
}
...
    void Character::print() 
    {
        cout << getCharClass() << " (HP:" << getHp() << "/DAMAGE-PHYICAL:" <<
            getPhysDmg() << "/DAMAGE-MAGICAL:" << getMagicDmg() << ")\n";
    }
...        
        int main()
        {
            Character player;
            player.classSelect();
            player.print();

            return 0;
        }

It seems to print the default case every time, no matter what I input with 'cin'. For example, If I enter 2 it still prints "Warrior (HP .....". Any help would be appereciated.

Nicolas
  • 3
  • 1
  • 4
    You haven’t written any break statement within case of switch – Rajan Maheshwari Oct 16 '17 at 14:12
  • 2
    I recommend you [find a good beginners book or two to read](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It should explain everything about this issue (and many more you might encounter!) – Some programmer dude Oct 16 '17 at 14:12
  • 4
    Don't get used to abusing the comma operator. Separate your statements with a semicolon to minimize surprises. – molbdnilo Oct 16 '17 at 14:16

3 Answers3

0

You should add a break in each case...

switch (c)
{
case 1:  
   m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
   break;
case 2:  
   m_class = "Mage", hp = 20, physDmg = 1, magicDmg = 10;
   break;
case 3:  
   m_class = "Battlemage", hp = 25, physDmg = 7, magicDmg = 7;  
   break;
default: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
    cout << "You chose : " << m_class << endl;
}
Michael
  • 408
  • 3
  • 13
0

you need to use

break;

for a case instruction to end : your current code will go until the end of the loop and run the default each time

    switch (c)
    {
    case 1:  m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1; break;
    case 2:  m_class = "Mage", hp = 20, physDmg = 1, magicDmg = 10; break;
    case 3:  m_class = "Battlemage", hp = 25, physDmg = 7, magicDmg = 7; break; 
    default: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1; break;
        cout << "You chose : " << m_class << endl;
    }

should give you what you want

reference

WNG
  • 3,705
  • 2
  • 22
  • 31
0

switch behaves like a indexable goto. case-s are just "labels" and when "hitting" the next case the code will merely fall through. If you want to end a certain switch case you have to terminate it with a break. Like this:

switch (c)
{
case 1:  m_class = ...; break;
case 2:  m_class = ...; break;
default: ...;
}
datenwolf
  • 159,371
  • 13
  • 185
  • 298