Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

c++ - cin.clear() doesn't reset cin object

I have the following loop. It should read numbers until EndOfFile, or the user input -999

int arr[100];

int index;

for (index = 0; index < 100; index++)
{
 cin >> arr[index];
 if (!cin)
 {
  cin.clear();
  index--;
  continue;
 }
 if (arr[index] == -999)
 {
     break;
 }
}

When the user input an invalid thing, such as some chars, this loop is being repeated for ever without clearing the error state or stopping.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After calling clear, you must also somehow remove the invalid input from the stream. Here is one way:

 cin >> arr[index];
 if (!cin)
 {
  cin.clear();
  std::string ignoreLine; //read the invalid input into it
  std::getline(cin, ignoreLine); //read the line till next space
  index--;
  continue;
 }

It's because when cin fails to read the invalid input, it remains there in the stream. It has to be removed, by some means. I just read and ignore it.

You can also use ignore as:

cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(),' ');

which is better in my opinion provided inputs are space separated (and if you don't want to inspect the invalid input). The online doc says:

istream::ignore

istream& ignore( streamsize n = 1, int delim = EOF );

Extract and discard characters

Extracts characters from the input sequence and discards them.

The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...