【发布时间】:2011-03-01 09:02:05
【问题描述】:
好的,我有一个问题,我不再使用字符串进行选择,所以现在我使用整数。当用户输入一个数字时,游戏就会进行。如果他们输入了错误的字符,它应该给出 else 语句,但是如果我输入一个字母或字符,系统会进入无限循环效果然后崩溃。即使用户定义了变量的类型,有没有办法给出 else 语句。
// action variable;
int c_action:
if (c_action == 1){
// enemy attack and user attack with added effect buffer.
///////////////////////////////////////////////////////
u_attack = userAttack(userAtk, weapons);
enemyHP = enemyHP - u_attack;
cout << " charging at the enemy you do " << u_attack << "damage" << endl;
e_attack = enemyAttack(enemyAtk);
userHP = userHP - e_attack;
cout << "however he lashes back causing you to have " << userHP << "health left " << endl << endl << endl << endl;
//end of ATTACK ACTION
}else{
cout << "invalid actions" << endl;
goto ACTIONS;
}
【问题讨论】:
-
您还没有显示读取用户输入的行。
-
我想 'goto ACTIONS' 可以替换为适当的函数调用。
-
你能对格式做点什么吗?这太可怕了,我只是不想打扰。此外,我看到了 else 但没有 if。
-
goto ACTIONS; // here be raptors -
看起来我们三个人同时修复了代码布局,哈哈。这让我的眼睛流血了。
标签: c++ int if-statement