My code is written twice for uncomprehensible reasons

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

    You forgot about the newline. cin reads every character, which includes the newline you type after typing your character. If you don't want this behaviour, you have to specifically check for newline.

    while (letter!= 'X')
    {
          if (letter == '\n')
          {
              letter = cin.get();
              continue;
          }
          cout<

提交回复
热议问题