我自己做了个混合运算程序,但是出现如图错误
上面就是错误,第二个输入,程序自动输入了bbb,无语~~
#include<iostream.h>
#include<time.h>
#include<stdlib.h>
int l=100;
int main()
{
int x,//减数
y,//被减数
n,//测试者输入的结果
w,//运算类型选择
temp,
count1=0,//对正确结果的统计
count=0;//对所有结果的统计
char m;
float A,z;//正确结果;
srand(time(NULL));
x=rand()%l;
y=1+rand()%l;
w=1+rand()%4;
if(x<y)
{
temp=x;
x=y;
y=temp;
}
do
{
switch(w)//随机选择运算类型
{
case 1:
z=x+y;
cout<<x<<" + "<<y<<" = ?"<<endl;//调用加法运算
break;
case 2:
z=x-y;
cout<<x<<" - "<<y<<" = ?"<<endl;//调用减法运算
break;
case 3:
z=x*y;
cout<<x<<" * "<<y<<" = ?"<<endl;//调用乘法运算
break;
case 4:
z=int(100*static_cast<float>(x/y)+0.5)/100.0;//限制计算机得出结果的位数
cout<<x<<" / "<<y<<" = ?"<<endl;//调用除法运算
default:
break;
}
cout<<"Please intput answer: ";
cin>>n;
count++;//统计所有计算结果的次数
if(n==z)
{
x=rand()%l;
y=1+rand()%l;
w=1+rand()%4;
if(x<y)//避免在减法中,出现负数结果
{
temp=x;
x=y;
y=temp;
}
cout<<"aaa"<<endl;
count1++;//统计正确结果的次数
if(count1%10==0)//当测试者作对10道题时,给出判断是否继续
{
cout<<"Do or break (Y=continue or N=break)?"<<endl;
cin>>m;
if((m=='Y')||(m=='y'))
continue;
if((m=='N')||(m=='n'))
break;
}
}
else
cout<<"bbb"<<endl; //就是这里,程序把它当结果自动输入了,郁闷
}while((n=cin.get())!=EOF);
A=100*count1/count;//计算成绩
cout<<"You got "<<A<<" points in subtration exam."<<endl;
if(A<75)
cout<<"You must study hard!"<<endl;
else
cout<<"You did better,but don't pride off!"<<endl;
}
#include<time.h>
#include<stdlib.h>
int l=100;
int main()
{
int x,//减数
y,//被减数
n,//测试者输入的结果
w,//运算类型选择
temp,
count1=0,//对正确结果的统计
count=0;//对所有结果的统计
char m;
float A,z;//正确结果;
srand(time(NULL));
x=rand()%l;
y=1+rand()%l;
w=1+rand()%4;
if(x<y)
{
temp=x;
x=y;
y=temp;
}
do
{
switch(w)//随机选择运算类型
{
case 1:
z=x+y;
cout<<x<<" + "<<y<<" = ?"<<endl;//调用加法运算
break;
case 2:
z=x-y;
cout<<x<<" - "<<y<<" = ?"<<endl;//调用减法运算
break;
case 3:
z=x*y;
cout<<x<<" * "<<y<<" = ?"<<endl;//调用乘法运算
break;
case 4:
z=int(100*static_cast<float>(x/y)+0.5)/100.0;//限制计算机得出结果的位数
cout<<x<<" / "<<y<<" = ?"<<endl;//调用除法运算
default:
break;
}
cout<<"Please intput answer: ";
cin>>n;
count++;//统计所有计算结果的次数
if(n==z)
{
x=rand()%l;
y=1+rand()%l;
w=1+rand()%4;
if(x<y)//避免在减法中,出现负数结果
{
temp=x;
x=y;
y=temp;
}
cout<<"aaa"<<endl;
count1++;//统计正确结果的次数
if(count1%10==0)//当测试者作对10道题时,给出判断是否继续
{
cout<<"Do or break (Y=continue or N=break)?"<<endl;
cin>>m;
if((m=='Y')||(m=='y'))
continue;
if((m=='N')||(m=='n'))
break;
}
}
else
cout<<"bbb"<<endl; //就是这里,程序把它当结果自动输入了,郁闷
}while((n=cin.get())!=EOF);
A=100*count1/count;//计算成绩
cout<<"You got "<<A<<" points in subtration exam."<<endl;
if(A<75)
cout<<"You must study hard!"<<endl;
else
cout<<"You did better,but don't pride off!"<<endl;
}
[此贴子已经被作者于2006-6-5 11:31:35编辑过]