My code is written twice for uncomprehensible reasons

前端 未结 6 796
一向
一向 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条回答
  •  闹比i
    闹比i (楼主)
    2021-01-24 11:44

    as everyone already mentioned, cin will append the newline marker \n every time you hit enter. another solution is to place cin.ignore(); after every cin.get();.

    #include 
    
    using std::cin;
    using std::cout;
    using std::endl;
    
    int main()
    {
        char letter;
    
        letter = cin.get();
        cin.ignore();
        while (letter!= 'X')
        {
              cout<

提交回复
热议问题