Sign in to follow this  

cleaning the cin buffer

This topic is 4726 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

This topic is 4726 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this