一个关于C++判断输入数据的问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 11:26:06
int humanmove(const vector<char>& board)
{
int move;

do{
cout<<"ok,where do you want to go?(0-8)"<<endl;
cin>>move;
}while(move<0||move>8);

while(!islegal(board,move))
{
cout<<"foolish human,that place has been occupied,choose an other place:\n"<<endl;
cin>>move;
}
return move;

}

如上源代码所示,我想实现的是,如果输入的是一个字符或者是浮点数什么的,程序不会陷入死循环。。回答的同学直接改好代码就行。万分感谢!
1楼的大哥。。能讲解下我用的dev_c++..没有<typeinfo.h>这个头文件啊。。。
3楼的朋友,int humanmove(const vector<char>& board)
{
int move;

do{
cout<<"ok,where do you want to go?(0-8)"<<endl;
cin>>move;
if(typeid(move).name()!="int"){continue;}
}while(move<0||move>8||(!cin));

给你参考
int main()
{
int move;
do
{
cout<<"STAR!";
cin>>move;
if(!cin)
{cerr<<"类型错误!"<<endl;
cout<<"请重新运行程序!";
return -1;
}
}while(move<3);
return 0;
}

#include<typeinfo.h>

int humanmove(const vector<char>& board)
{
int move;

do{
cout<<"ok,where do you want to go?(0-8)"<<endl;
cin>>move;
if(typeid(move).name()!="int"){continue;}
}while(move<0||move>8);

while(!islegal(board,move))
{
cout<<"foolish human,that place has been occupied,choose an other place:\n"<<endl;
cin>>move;
}
return move;

}