My code is written twice for uncomprehensible reasons

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

    It happens because cin.get() reads new-line character too. Try to press Enter without any symbols or type some string, like abc. You need to handle it, e.g.:

    while (letter = cin.get()) {
        if (!isalpha(letter)) { continue; }
        // handling user inputted alpha
    }
    

提交回复
热议问题