这是我的第一篇文章。我是一位业余程序员,他刚刚学习了类和继承。所以我想我会尝试制作基于文本的RPG来练习我所学到的知识。然而,有一个即时通讯问题,在这个功能我switch语句:Switch Statement不响应Cin输入
...
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;
}
好像每次我输入与“CIN”打印默认情况下,不管。例如,如果我输入2仍然打印“勇士(HP .....”。 任何帮助将appereciated。
您没有开关 –
的情况下,内写任何break语句,我建议您[找到一个很好的初学者两本书阅读](http://stackoverflow.com/questions/388242/the -definitive-C-书指南和列表)。它应该解释有关此问题的所有内容(以及更多可能遇到的问题!) –
不要习惯滥用逗号运算符。用分号分隔您的语句,以最大限度地减少意外。 – molbdnilo