缓存清不了,不知道为什么。
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
int main(){
int a,char b;
srand((unsigned)time(0));
do{
for(int i=0;i<1;i++){
double ran_num=rand()%10;
do{
cout<<"please enter a number between 1 to 10.\n";
cin.clear();
cin.sync();
cin>>a;
if(a==0){
cout<<"data type error!\n";
b='y';
continue;}
if(a<=0||a>=10){
cout<<"input a number between 1 to 10!\n";
b='y';
continue;}
if(a!=ran_num&&a>ran_num){
cout<<"please input a lesser number\n";}
if(a!=ran_num&&a<ran_num){
cout<<"please input a lager number\n";}
}while(a!=ran_num);
cout<<"Gongratulation!\n";
}
do{
cout<<"\nContinue(yes or no)?\n";
b=getche();
}while(b!='y'&&b!='n');
}while(b=='y');
}
各位大哥可以复制下来,到vc6中运行下试试。由于我才学c++,有很多问题啊~~~~
这个是个简单的猜数游戏,我想控制不能输入字母,但一旦输入字母就会出现无限循环的情况,应该是缓存没清,但我写了cin.clear();cin.sync();的呀,真不知道为什么了~~~~~;还有就是当猜对时,要输入“y”,才会出现 “Gongratulation!”,然后就立马重复游戏了......这个顺序不对啊,应该是出现“Gongratulation!”再问“Continue(yes or no)?”,输入“y”之后才继续游戏啊......
麻烦大家帮帮忙吧,我真不知道是哪出问题了~~~~~谢谢拉~~~~~
#include<stdlib.h>
#include<time.h>
#include<conio.h>
int main(){
int a,char b;
srand((unsigned)time(0));
do{
for(int i=0;i<1;i++){
double ran_num=rand()%10;
do{
cout<<"please enter a number between 1 to 10.\n";
cin.clear();
cin.sync();
cin>>a;
if(a==0){
cout<<"data type error!\n";
b='y';
continue;}
if(a<=0||a>=10){
cout<<"input a number between 1 to 10!\n";
b='y';
continue;}
if(a!=ran_num&&a>ran_num){
cout<<"please input a lesser number\n";}
if(a!=ran_num&&a<ran_num){
cout<<"please input a lager number\n";}
}while(a!=ran_num);
cout<<"Gongratulation!\n";
}
do{
cout<<"\nContinue(yes or no)?\n";
b=getche();
}while(b!='y'&&b!='n');
}while(b=='y');
}
各位大哥可以复制下来,到vc6中运行下试试。由于我才学c++,有很多问题啊~~~~
这个是个简单的猜数游戏,我想控制不能输入字母,但一旦输入字母就会出现无限循环的情况,应该是缓存没清,但我写了cin.clear();cin.sync();的呀,真不知道为什么了~~~~~;还有就是当猜对时,要输入“y”,才会出现 “Gongratulation!”,然后就立马重复游戏了......这个顺序不对啊,应该是出现“Gongratulation!”再问“Continue(yes or no)?”,输入“y”之后才继续游戏啊......
麻烦大家帮帮忙吧,我真不知道是哪出问题了~~~~~谢谢拉~~~~~
...全文



