64,662
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<vector>
#include<stdexcept>
#include<cstdio>
using namespace std;
void keep_window_open()
{
cin.clear();
cout<<unitbuf; //就是这个地方,我想将输入流彻底清空,不知道用什么方法
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
cout <<ch <<endl;
return;
}
int main()
try{
vector<int> v;
int x;
while(cin>>x)
{
v.push_back(x);
}
for(int i=0;i<=v.size();i++)
cout<<"v["<<i<<"]=="<<v.at(i)<<endl;
}catch (exception& oor) {
cerr << "Out of Range error: " << oor.what() << endl;
keep_window_open();
return 1;
}catch(...){
cerr<<"exception :something went wrong\n";
return 2;
}
#include<iostream>
#include<vector>
#include<stdexcept>
#include<cstdio>
using namespace std;
void keep_window_open()
{
cin.clear();
cin.sync();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
cout <<ch <<endl;
return;
}
int main() {
try{
vector<int> v;
int x;
while(cin>>x)
{
v.push_back(x);
}
for(int i=0;i<=v.size();i++)
cout<<"v["<<i<<"]=="<<v.at(i)<<endl;
return 0;
} catch (exception) {
cerr << "Invalid input! "<< endl;
keep_window_open();
return 1;
} catch (...) {
cerr<<"exception :something went wrong\n";
return 2;
}
}
//11
//22
//s
//v[0]==11
//v[1]==22
//Invalid input!
//Please enter a character to exit
//a
//a
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;
void keep_window_open()
{
cout << "Please enter a character to exit\n";
char ch;
rewind(stdin);
ch=getchar();
cout << ch <<endl;
return;
}
int main() {
vector<int> v;
int x,r;
while (1) {
r=scanf("%d",&x);
if (1==r) {//输入一个整数回车
v.push_back(x);
} else if (0==r) {
cerr << "Invalid input!" << endl;
keep_window_open();
return 1;
} else if (EOF==r) {//输入Ctrl+Z回车
break;
} else {
cerr << "exception :something went wrong\n";
return 2;
}
}
for (int i=0;i<=v.size();i++)
cout << "v[" << i << "]==" << v.at(i) << endl;
return 0;
}
//11
//22
//s
//Invalid input!
//Please enter a character to exit
//a
//a