Cin.ignore **CODE PROVIDED**

Cin.ignore() is acting very weird. If I input the following input sequence to this program:

2
5 10
{extra input requested here by cin.ignore()}
2 12

I am met with an extra input request. Using GDB, I found that the cin.ignore() on line 12 on the second iteration of the while loop is causing this input request. Can someone tell me why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>
#include <limits>
using namespace std;

int main() {
  //code
  int tests,n,m; cin >> tests;
  bool a = false;
  while(tests--){
      string s, token;
      if(!a) cin.ignore(numeric_limits<streamsize>::max(), '\n');
//cin.ignore(100, '\n'); //Optionally, this works without using numeric_limits.
      getline(cin, s);
      for(int i=0;i<s.length();++i){
          //Space
          if(s[i] != ' '){
              token +=s[i];
          } else {
            n = stoi(token);
            token.clear();
          }
      }
      m = stoi(token);
      if(n%2==0){ cout<<((n/2))*m<<endl;}
      else {cout<<ceil((float) n/2)*m<<endl;}
      a = true;
  }
  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath>
#include <limits>
#include <string>
using namespace std;

int main()
{
	int tests; 
	cin >> tests;
	
	if (cin.peek() == '\n')
	{
		std::cout << "IM BLACK YALL!\n";
	}

	bool a = false;
	while (tests--) 
	{
		string s;
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		getline(cin, s);
		if (cin.peek() == '\n')
		{
			std::cout << "IM BLACK YALL!\n";
		}
	}
}


Here's a simpler version of your code. Your code easily breaks BTW and needs a #include <string> at the top.

When you first input something, you put in "2\n" (you press 2 and then press enter. You go into the loop, it sees the "\n" and it skips over it and now you're on getline(cin, s). You input, let's say, "Samantha\n". Getline takes "Samantha\n" - the whole thing. It doesn't leave behind "\n" like cin >> had previously.

So now you're on your next loop, but it actually stops for cin.ignore this time, since there wasn't a '\n' in the buffer as with cin >>. So now, you input something, thinking you're inputting due to getline(), when in fact the program is just waiting for '\n' so it can move it, ignoring any input you type in before that.
Hello danc2,

As zapshe has pointed out you are missing the header file "string", very important.

When I ran your program I had no idea what to do when faced with a blank screen. Your code:
int tests,n,m; cin >> tests; would work better as:
1
2
3
4
5
int tests{},n{},m{};

std::cout << "How many tests do you want? ";
cin >> tests;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

Always a good idea to precede any input with a prompt to let users know what needs to be entered.

It is also a good to avoid single letter variable names. It may not seem much of a problem now, but as your programs grow it will be harder to keep track of. For a variable name a noun that describes what it is for or what it does works best.

If at all possible it is best to follow the formatted input, i.e., cin >> tests; with the ignore statement. You can put the ignore statement before a "std::getline", but not a good habit to get into as sometimes it does not work the way that you want.

When I moved the ignore statement out of the while loop the program worked as it should. Refer to above code.

Hope that helps,

Andy

@zaphse and @Handy Andy

Wow! Thank you so much to the both of you.

Zaphse, your example really helped me understand what was going on. It makes sense now why it was stopping, waiting for a '\n'!

Handy, thank you. It was for a competitive programming thing, so I did not, but I probably should have for this forum. Thanks for the beginner tips and solution. I moved the cin.ignore and it worked perfectly!
Topic archived. No new replies allowed.