怎么防止不正确的输入
//这是我初学做的第一个小游戏,很严重的BUG就是在INT型中如果输入char就挂了...
//还有就是char型输入的时候如果输入两个也会出
//希望高手能告诉我怎么做好吗?最好能帮我修改一下 谢谢...
//如果有其他错误能帮我指出也最好了...
#include "stdafx.h "
#include <time.h>
class hero
{
public:
string name;
int hp;
int att;
int def;
int dex;
};
bool htt(hero htt1,hero htt2)
{
return (rand()%10+(htt1.dex -htt2.dex))> 3;
}
int assault(hero ass1,hero ass2)
{
return (ass1.att*2-ass2.def+rand()%3);
}
int main()
{
bool start=1;
while(start)
{
cout < < "***********************************************\n "
< < "现在开始开始玩一个游戏,你可以压注自己看中的战士.\n如果该战士赢了,你可以得到你押的双倍钱.如果战士输了你钱就归我啦.\n "
< < "一开始的时候你有100枚金币,等你赚到1000枚的时候游戏结束\n ";
int gold=100;
while(gold <1000)
{
srand(time(NULL));
hero pl={ "红方 ",rand()%100+100,rand()%10+10,rand()%10+10,rand()%10+10};
hero npc={ "蓝方 ",rand()%100+100,rand()%10+10,rand()%10+10,rand()%10+10};
cout < < "--------------------------\n ";
cout < <pl.name < <endl < < "HP: " < <pl.hp < < " " < < "攻击力: " < <pl.att < < "防御力: " < <pl.def
< < "敏捷: " < <pl.dex < <endl;
cout < <npc.name < <endl < < "HP: " < <npc.hp < < " " < < "攻击力: " < <npc.att < < "防御力: " < <npc.def
< < "敏捷: " < <npc.dex < <endl;
bool right=0;
int bout=0;
char man;
cout < < "你当前有 " < <gold < < "枚金币\n你可以选择押红方或者蓝方.\n请问你要押给谁呢? 红(R)蓝(B) ";
while(cin> > man&&man!= 'R '&&man!= 'r '&&man!= 'b '&&man!= 'B ')
{
cout < < "请重新输入 红(R)蓝(B) ";
}
int sel=0;
do
{
cout < < "你要压多少?1-50 ";
cin> > sel;
}while(sel <1||sel> 50);
while (pl.hp> 0&&npc.hp> 0)
{
++bout;
if(bout%2!=0)
{
if(htt(pl,npc))
{
cout < <pl.name < < "对 " < <npc.name < < "进行了攻击 ";
npc.hp-=assault(pl,npc);
if(npc.hp <=0)
{
cout < <npc.name < < "死亡\n ";
if(man!= 'r ')
right=false;
else
right=true;
}
else
cout < <npc.name < < "还剩HP: " < <npc.hp < <endl;
}else
{
cout < <pl.name < < "对 " < <npc.name < < "进行了攻击 ";
cout < <pl.name < < "没有击中 " < <npc.name < <endl;
}
}else
{
if(htt(npc,pl))
{
cout < <npc.name < < "对 " < <pl.name < < "进行了攻击 ";
pl.hp-=assault(npc,pl);
if(pl.hp <=0)
{
cout < <pl.name < < "死亡\n ";
if(man!= 'b ')
right=false;
else
right=true;
}
else
cout < <pl.name < < "还剩HP: " < <pl.hp < <endl;
}else
{
cout < <npc.name < < "对 " < <pl.name < < "进行了攻击 ";
cout < <npc.name < < "没有击中 " < <pl.name < <endl;
}
}
}
if (right!=true)
{
cout < < "你失败了!被扣除 " < <sel < < "枚金币 ";
gold-=sel;
}else
{
cout < < "恭喜你赢得了 " < <sel < < "枚金币 ";
gold+=sel;
}
}
}
system( "pause ");
}
[解决办法]
int i;
cin > > i;
改成
cin > > i;
while(!cin)
{
cin.ingore(1000);
cin.clear();
cout > > "Please re input ";
cin > > i;
}
[解决办法]
或者自己把所有的用户输入都当作字符读进来,然后自己判断并作转换
[解决办法]
iostream库中的cin有很多功能,可以去查一下
[解决办法]
//这是我初学做的第一个小游戏,很严重的BUG就是在INT型中如果输入char就挂了...
//还有就是char型输入的时候如果输入两个也会出
//希望高手能告诉我怎么做好吗?最好能帮我修改一下 谢谢...
//如果有其他错误能帮我指出也最好了...
//#include "stdafx.h "
#include <time.h>
#include <string>
#include <iostream>
using namespace std;
class hero
{
public:
string name;
int hp;
int att;
int def;
int dex;
};
bool htt(hero htt1,hero htt2)
{
return (rand()%10+(htt1.dex -htt2.dex))> 3;
}
int assault(hero ass1,hero ass2)
{
return (ass1.att*2-ass2.def+rand()%3);
}
int main()
{
bool start=1;
while(start)
{
cout < < "***********************************************\n "
< < "现在开始开始玩一个游戏,你可以压注自己看中的战士.\n如果该战士赢了,你可以得到你押的双倍钱.如果战士输了你钱就归我啦.\n "
< < "一开始的时候你有100枚金币,等你赚到1000枚的时候游戏结束\n ";
int gold=100;
while(gold <1000)
{
srand(time(NULL));
hero pl={ "红方 ",rand()%100+100,rand()%10+10,rand()%10+10,rand()%10+10};
hero npc={ "蓝方 ",rand()%100+100,rand()%10+10,rand()%10+10,rand()%10+10};
cout < < "--------------------------\n ";
cout < <pl.name < <endl < < "HP: " < <pl.hp < < " " < < "攻击力: " < <pl.att < < "防御力: " < <pl.def
< < "敏捷: " < <pl.dex < <endl;
cout < <npc.name < <endl < < "HP: " < <npc.hp < < " " < < "攻击力: " < <npc.att < < "防御力: " < <npc.def
< < "敏捷: " < <npc.dex < <endl;
bool right=0;
int bout=0;
char man;
cout < < "你当前有 " < <gold < < "枚金币\n你可以选择押红方或者蓝方.\n请问你要押给谁呢? 红(R)蓝(B) ";
while(cin> > man&&man!= 'R '&&man!= 'r '&&man!= 'b '&&man!= 'B ')
{
cout < < "请重新输入 红(R)蓝(B) ";
}
int sel=0;
do
{
cout < < "你要压多少?1-50 ";
cin > > sel;
while ( !cin )
{
cin.clear();
while ( cin.get() != '\n ')
{
continue;
}
cout < < "输入错误, 请从新输入: ";
cin > > sel;
}
}while(sel <1||sel> 50);
while (pl.hp> 0&&npc.hp> 0)
{
++bout;
if(bout%2!=0)
{
if(htt(pl,npc))
{
cout < <pl.name < < "对 " < <npc.name < < "进行了攻击 ";
npc.hp-=assault(pl,npc);
if(npc.hp <=0)
{
cout < <npc.name < < "死亡\n ";
if(man!= 'r ')
right=false;
else
right=true;
}
else
cout < <npc.name < < "还剩HP: " < <npc.hp < <endl;
}else
{
cout < <pl.name < < "对 " < <npc.name < < "进行了攻击 ";
cout < <pl.name < < "没有击中 " < <npc.name < <endl;
}
}else
{
if(htt(npc,pl))
{
cout < <npc.name < < "对 " < <pl.name < < "进行了攻击 ";
pl.hp-=assault(npc,pl);
if(pl.hp <=0)
{
cout < <pl.name < < "死亡\n ";
if(man!= 'b ')
right=false;
else
right=true;
}
else
cout < <pl.name < < "还剩HP: " < <pl.hp < <endl;
}else
{
cout < <npc.name < < "对 " < <pl.name < < "进行了攻击 ";
cout < <npc.name < < "没有击中 " < <pl.name < <endl;
}
}
}
if (right!=true)
{
cout < < "你失败了!被扣除 " < <sel < < "枚金币 ";
gold-=sel;
}else
{
cout < < "恭喜你赢得了 " < <sel < < "枚金币 ";
gold+=sel;
}
}
}
system( "pause ");
}
// 用的是C++ Primer 里的技法