Hi!

I read the clearing input buffer article on the top of the c++ forum but I don't really get it, and it doesn't work.

This is my problem, I know that cin is just like a file in that it has a buffer (correct me if I'm wrong, I think I'm right) and can use the same things that istream objects can use.

so, that (if I'm right) would mean that this is legal cin.ignore . but for some reason it's not... what's wrong? the error occurs in just the one spot and it seems to not be related to any I did in the program elsewhere.

just ask if you want to see the source

thanks,
Jt

Dani AI

Generated

Quick explanation and a reliable fix.

The compile-time error often comes from using the function name instead of calling it (writing cin.ignore instead of cin.ignore() or cin.ignore(args)). As showed, leftover characters (typically the newline after a formatted read) are what cause the next unformatted read to see an immediate character. Hard-coded counts like 1000 sometimes work, but they can fail (which explains "almost every time").

A robust, idiomatic pattern is to clear error flags when needed and then discard the rest of the current input line using std::numeric_limits. This always drops everything up to the next newline:

#include <iostream>
#include <limits>
#include <string>

int main() {
    int value;
    if (!(std::cin >> value)) {
        std::cin.clear(); // clear failbit if parsing failed
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // flush bad input
    } else {
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // remove remainder of the line
        std::string line;
        std::getline(std::cin, line); // now safe to read a full line
    }
}

Extra tips and pitfalls:

  • Prefer using std::getline for whole-line input and then parsing (e.g., std::stoi or std::istringstream) to avoid mixing formatted and unformatted reads.
  • If a read fails, call std::cin.clear() before ignore().
  • std::ws can be used when only leading whitespace should be skipped.
  • Avoid fixed-size ignores (like 1000) for production; numeric_limits::max() is safer.

This should resolve the intermittent behavior reported by and make input handling deterministic.

Recommended Answers

All 2 Replies

Remember cin.ignore has two parameters.

cin.ignore(1000, '\n');

This is to clear the input stream useful with while mixing cin.get(param1) and cin >> variable1.

Example 1

/*
	Purpose: threads/354626
	Name: Saith
	Date: 3/20/11
	*/

#include<iostream>
using namespace std;


int main(){

	int var1;
	char var2;

	cout << "Input some number.\n";
	cin >> var1;
	cout  << "Your number is "<< var1 << endl;

	cout << "Input another number.\n";
	cin.get(var2);
	cout  << "Your number is "<< var2 << endl;

	return 0;
}

Input:
1
// c // would have been the second input

Output:
1
[blank space]


and will not bother pausing for a keystroke due to a character already in cin (the '\n' character). If you add the cin.ignore(para1, para2); syntax, the '\n' character will be removed from the stream input along with any other value.

Example 2

/*
	Purpose: threads/354626
	Name: Saith
	Date: 3/20/11
	*/

#include<iostream>
using namespace std;


int main(){

	int var1;
	char var2;

	cout << "Input some number.\n";
	cin >> var1;
	cin.ignore(1000, '\n');
	cout  << "Your number is "<< var1 << endl;

	cout << "Input another number.\n";
	cin.get(var2);
	cout  << "Your number is "<< var2 << endl;

	return 0;
}

Input:
1
c
Output:
1
c

commented: Nice :) +36

Thank you, this has been plaguing me for a long time and it's very annoying. However, this helped me a lot and now it works almost every time.

Thanks again,
jt

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.