• Advertisement
Sign in to follow this  

cleaning the cin buffer

This topic is 4794 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello, since the search doesn't work, I have no choice then to ask my question here :) I'm making an exercise for my school and came upon a problem, when I execute my program, a cin-line is executed twice like this :
Gelieve de titel in te geven (max 28 karakters) :
Gelieve de titel in te geven (max 28 karakters) :


sorry for showing the dutch language, but I hope you see what I mean. it's like the first time there was still a "enter" stuck in the buffer this is my main :
void main()
{
	cout << "Examenopdracht 14 :" << endl << endl ;

	Venster_met_titel RH ;

	RH.ZetLengte() ;
	RH.ZetBreedte() ;
	RH.ZetTitel() ;
	RH.Teken() ;
	
	RH.Breder() ;
	RH.Breder() ;
	RH.Langer() ;
	RH.Langer() ;
	RH.ZetTitel() ;
	RH.Teken() ;
}


that cin-line is within the member function ZetTitel() and before I call ZetTitel() I call also ZetLengte() and ZetBreedte() as you can see in the main() :
void Rechthoek::ZetLengte()
{
	cout << "Gelieve de lengte op te geven : " ;
	lengte = invoer_int() ;
}

void Rechthoek::ZetBreedte()
{	
	cout << "Gelieve de breedte op te geven : " ;
	breedte = invoer_int() ;
}


and this is the ZetTitel() :
void Venster_met_titel::ZetTitel()
{
	if (breedte != 0)
	{
		do
		{
			cout << "Gelieve de titel in te geven (max " << (breedte-2) << " karakters) :" << endl ;
			cin.getline(titel, 30) ;
		} while (((int)strlen(titel) <= 0) || ((int)strlen(titel) > (breedte-2))) ;
	}
	else
	{
		cout << "Breedte werd nog niet ingevoerd!" << endl ;
		ZetBreedte() ;
		do
		{
			cout << "Gelieve de titel in te geven (max " << (breedte-2) << " karakters) :" << endl ;
			cin.getline(titel, 30) ;
		} while (((int)strlen(titel) <= 0) || ((int)strlen(titel) > (breedte-2))) ;
	}
}


I hope some1 can help me...

Share this post


Link to post
Share on other sites
Advertisement
There is no portable way to clear the input buffer(s). You can however clear the streambuf that belongs to std::cin.


std::cin.ignore(std::cin.rdbuf()->in_avail());




Share this post


Link to post
Share on other sites
The sync member function should remove any unread characters from the cin buffer as I understand it.

Enigma

Share this post


Link to post
Share on other sites
i tried the sync-member but that didn't work,
but :

std::cin.ignore(std::cin.rdbuf()->in_avail());

workend for me

thx for the help guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement