Below are the two lines code. Plz give a detailed explanation of these two lines

while (cin.get() != '\n')
continue;

Recommended Answers

All 5 Replies

What do YOU tink they mean?

It's a WRONG attempt to eat the rest of input line on cin (for example, you get the 1st word and you want to skip other stuff).
if cin is attached to a file (not to console), you may get cin.eof condition before '\n' so you never get '\n' in these two lines of code.
Right solution (one of);

while (cin && cin.get() != '\n')
    continue;
// or simple
while (cin && cin.get() != '\n')
    ;

It's a WRONG attempt to eat the rest of input line on cin (for example, you get the 1st word and you want to skip other stuff).
if cin is attached to a file (not to console), you may get cin.eof condition before '\n' so you never get '\n' in these two lines of code.
Right solution (one of);

while (cin && cin.get() != '\n')
    continue;
// or simple
while (cin && cin.get() != '\n')
    ;

The correct solution to clearing the input stream is explained here.

Hi,
I am Rammohan from Bangalore and working as a Technical lead in big IT firm .
Solution for your answer is follows:


The Syntax of the snippet code is 100% correct.
It mean that continue your while loop until the input reaches to the next line...
If you press enter key In your input stream then the loop will come out otherwise it will contnue....

Regards,
Rammohan Alampally,
<snip false signature>

I did not invent a new (incorrect) method to flush input stream.
It was a correct version of the original snippet (skip a rest of a line).

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.