#include
#include
#include
using namespace std;
在输入两个或者三个正整数的时候没问题,但是数日其他的时候虽然提示了错误,但是并不能cin>>input,而是直接跳出了循环并且显示了press any key to continue?求大神给我说一下哪里出了错~~
int main()
{
int out();
cout<<"请输入两个或者三个正整数a, b, [c],用空格隔开"<<endl;
out();
return 0;
}
int out()
{
unsigned int biggest(unsigned int a,unsigned int b,unsigned int c = 0);
unsigned int b;
char input;
vector a;
while(1)
{
while(cin>>b)
{
a.push_back(b);
}
if(a.size() == 2)
{
cout<<"max is "<<biggest(a[0], a[1])<<endl;
}
else if(a.size() == 3)
{
cout<<"max is "<<biggest(a[0], a[1], a[2])<<endl;
}
else
{
try
{
throw runtime_error("必须有两个或者三个数的输入!") ;
}
catch(runtime_error err)
{
cout<<err.what()<<endl;
cout<<"错误,是否重新输入? 请输入Y 或者N"<<endl;
cin>>input;
if(input == 'Y')
{
out();
}
else
{
return 0;
}
}
}
if(((a.size() == 2 )|| (a.size() == 3)))
{
break;
}
}
return 0;
}
unsigned int biggest(unsigned int a,unsigned int b,unsigned int c = 0)
{
if(a < b)
{
a = b;
}
if(a < c)
{
a = c;
}
return a;
}
catch里面
cin>>input;前面加上(不加的话,输入缓冲区里会有遗留的字符,cin>>input会直接试图读取该字符,然后没读到执行了else分支)
cin.clear();//是用来更改cin的状态标示符的。
cin.sync();//是用来清除缓存区的数据流的。
而且有几个不明白的地方:
1.楼主如果要多次输入求最大值的话,应该每次循环开始的时候把vector清空
2.这个while(cin>>b)循环楼主是怎么跳出的?莫非是用ctrl+z?