简体   繁体   中英

Behaviour of cin.get() and cin.put()

The following is a code in C++ which takes a letter as an input and gives it as an output. This continuous until doesn't press Ctrl+C ie ^C .

#include<iostream>
using namespace std;
int main(void)
{
    char c;
    do
    {
        c = cin.get();
        cout.put(c);
    }while((c = cin.get())!='\0');
    return 0;
}

The problem is the following output:-

hello
hlo

hello
el

I know that why the first output is coming the way it coming but what about the second? Also, why are there two new lines when I am hitting enter key only once. Can I get some help?

The first time, cin.get() blocks the thread inside the do-while loop code. After you have gotten your input, you press enter and you find that 2 linebreaks appear. This is because cin.get() inside the do-while loop code has been blocking, and as you press enter, c will become '\\n' . What happens next is cout.put('\\n'); .

At this point, you have the cin.get() in the do-while condition blocking your thread, so you end up skipping the h , the first l and the o .

Look at the loop conditiion - you invoke cin.get() two times per each loop iteration. The missing characteres are compared to '\\0' and discarded.

As you know, cin.get() is being called twice in the loop, while cin.put(c) only once. Since you typed hello twice, and one character is skipped each time, the sequence of characters output is: (the characters skipped are in the parentheses): h (e) l (l) o (h) e (l) l (o)

Two key points should be kept in mind for this problem. These are:-

  1. When we press return key at the end of the string, it is also stored in the buffer and pushed along with all the characters stored in it onto the standard output.
  2. The output depends on whether there are en even or odd number of characters in the string ( including the return key )

Say that the string hello! is entered and return is hit. Then, we again enter hello! and hit the return key. In both the cases, total number ( in either case ) of characters is 7 and not 6 . In this case the output is:-

hello!
hlo

hello!
el!_

where _ denotes the cursor that will be displayed. This is because the return key hit after the first string was read by the cin.get() command within the do-while loop and hence it was printed. After that, the control went to the do-while condition and it read the h of the second string. This was the reason h isn't printed in the second output. After that every even character is displayed. This is the reason that ! is printed. After that the return key is detected by the do-while condition which pauses the execution at the _ position.

Now, if you hit return key once, you get return two times because once return is entered and the other time, it is the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM