get - order of execution in do/while loop -


i took following example literally walter savitch absolute c++ book. works (as 1 expect scholar such walter savitch). however, puzzled why explain after code citation:

cout << "enter line of input , echo it:\n"; char symbol; {     cin.get(symbol);     cout << symbol; } while (symbol != '\n'); cout << "that's demonstration.\n"; 

possible output follows:

enter line of input , echo it: do 1 2    34 do 1 2    34 that's demonstration. 

my problem following. in going through loop once, cin.get(symbol) find 1 character @ time, , cout output 1 character consequently. then, if input wasn't '\n' character, go loop second time, , on, until input equals '\n'.

however, in executing code, input seems read @ once, , copied @ once. how can happen if every input character needs checked equal '\n'?

final point, stating obvious: question not pertain code way not syntactical. puzzled happens during compilation and/or execution of simple code present above.

hopefully can me out!

thanks.

your observation correct. seems like input read @ once , then, printed out @ once. reason when output printed, goes buffer gets printed when buffer reaches size, or whenever code done. in latter case, output stream closed , gets flushed. practically speaking, code being printed buffer.


Comments