开发者

C++: Do I need to use cin.get() before I use cin.ignore?

Does cin.ignore(100,'\n') work if I just use standard cin through the stream?

Example:

cin >> temp;  //enter key input
if(ci开发者_如何学Pythonn.fail())
{
  cin.clear();
  cin.ignore(100,'\n');
}

or do I have to use cin.get()?

Example:

temp=cin.get();
if(cin.fail())
{
  cin.clear();
  cin.ignore(100,'\n');
}

Or will these codes produce identical results?


Ah, stupid me, I made a function to call the clearing

void clear()
{
  cin.clear();
  cin.ignore(100,'\n');
}

and in my program, it called function clear() more than once. In the cases where clear() was called twice in a row without input from cin, it prompted for input in a blank line.

I'm guessing this is what caused it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜