What is the difference between cin.ignore and cin.sync ?
What is the difference between cin.ignore and cin.sync ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
cin.ignorediscards characters, up to the number specified, or until the delimiter is reached (if included). If you call it with no arguments, it discards one character from the input buffer.For example,
cin.ignore (80, '\n')would ignore either 80 characters, or as many as it finds until it hits a newline.cin.syncdiscards all unread characters from the input buffer. However, it is not guaranteed to do so in each implementation. Therefore,ignoreis a better choice if you want consistency.cin.sync()would just clear out what’s left. The only use I can think of forsync()that can’t be done withignoreis a replacement forsystem ("PAUSE");:With
cin.ignore()andcin.get(), this could be a bit of a mixture:If there was a newline left over, just putting
ignorewill seem to skip it. However, putting both will wait for two inputs if there is no newline. Discarding anything that’s not read solves that problem, but again, isn’t consistent.