My code is written twice for uncomprehensible reasons

前端 未结 6 765
一向
一向 2021-01-24 10:45

This code is written in C++ and for reasons that I don\'t quite understand it is written twice. I would expect that after inputting a random char it would display the char once

6条回答
  •  醉话见心
    2021-01-24 11:41

    When you type in a character the new-line character (from pressing enter) is also in your input buffer.

    From the C-Reference:

    The delimiting character is not extracted from the input sequence if found, and remains there as the next character to be extracted from the stream (see getline for an alternative that does discard the delimiting character).

    Just use a cin.sync() after every cin.get() to clear the buffer and you should be good to go.

提交回复
热议问题