开发者

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

开发者 https://www.devze.com 2023-02-20 09:49 出处:网络
Does cin.ignore(100,\'\\n\') work if I just use standard cin through the stream? Example: cin >> temp;//enter key input

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

精彩评论

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

关注公众号