w*****j 发帖数: 49 | 1 here is a little script i wrote:
int main(){
int n;
if(!(cin>>n)){
int m;
cin>>m;
cout<
}
}
if i type in a letter when it asks me to give a value to integer to n, it
won't let me input m, and simply print out 0, which i assumes is the value
of m. Anybody here is willing to help me out a little bit? | X****r 发帖数: 3557 | 2 This is how istream works: you try to extract a number from it by
"cin >> n", but the next character is not a part of any number,
so istream refuses to extract it. The rouge character is kept in
the istream, and the error state of this istream object is set to
"fail". To resume extracting numbers from this istream, you have
to do both of the following:
1) clear the "fail" state of the istream by calling "cin.clear()"
2) remove the bad character(s) from the stream. The easiest
way is to call "cin
【在 w*****j 的大作中提到】 : here is a little script i wrote: : int main(){ : int n; : if(!(cin>>n)){ : int m; : cin>>m; : cout<: } : } : if i type in a letter when it asks me to give a value to integer to n, it
| w*****j 发帖数: 49 | 3 Thank you very much, it works.
【在 X****r 的大作中提到】 : This is how istream works: you try to extract a number from it by : "cin >> n", but the next character is not a part of any number, : so istream refuses to extract it. The rouge character is kept in : the istream, and the error state of this istream object is set to : "fail". To resume extracting numbers from this istream, you have : to do both of the following: : 1) clear the "fail" state of the istream by calling "cin.clear()" : 2) remove the bad character(s) from the stream. The easiest : way is to call "cin
|
|