• Popular Now

• 16
• 15
• 11
• 9
• 10

Archived

This topic is now archived and is closed to further replies.

Fun with streams

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

Recommended Posts

int main(void)
{
int temp;

cout<<"Press a key to continue"<<endl;
getchar();

cout<<"Again"<<endl;
getchar();

cout<<"And Again"<<endl;
getchar();

cout<<"O.K. now for a change, press a key"<<endl;
cin>>temp;

cout<<"Why didn''t input get blocked on this line?"<<endl;
getchar();

cout<<"End"<<endl;
return 1;
}

Can anybody tell me why getchar() doesn''t block input after using cin, and how to remedy the situation

Share on other sites
The cin is probably leaving the \n inside the input stream (which is put in when you press enter), and so the getchar() just retrieves the \n and doesnt block

Share on other sites
Thanks for the tip