#include<iostream>
#include<vector>
#include<stdexcept>
#include<stdio.h>
using namespace std;
void keep_window_open()
{
cin.clear(); //clear badbit flag
cin.sync(); //clear stdin stream
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; //out of range
}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;
}
위 코드는 C++ 예외 처리기 기능을 수행하는 것입니다.표준 스트림을 지우는 방법
그러나 프로그램은 우분투 11.04에서 stdin 스트림 지점을 지울 때 제대로 실행되지 않지만 Codeblocks에서는 Windows에서 잘 작동합니다. 뭐가 문제 야?
왜 "fflush (stdin);" 위도 "cin.clear(); cin.sync();"수 없습니다? 그리고 스트림을 지울 다른 방법이 있다면?
도움 주셔서 감사합니다.
왜 그렇게하고 싶습니까? –
왜 그렇게하고 싶습니까? 나는 모든 데이터를 읽는 것을 제외하고는 STDIN을 "제거"하는 좋은 방법이 있다고 생각하지 않습니다. –