My code is written twice for uncomprehensible reasons

前端 未结 6 911
一向
一向 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:27

    You are reading every character with the unformatted get() function, including the newline character each time you hit return.

    Depending on what you're trying to do, you could use formatted input (cin >> c) to skip all whitespace; or you could test each character and ignore things like newline that don't interest you; or you could use getline(cin, some_string) to read a whole line, and then process that.

提交回复
热议问题