Visual c++ 检测输入键C++;

Visual c++ 检测输入键C++;,visual-c++,Visual C++,我试图屏蔽用户输入并将其存储在变量中。当我按enter键时,程序应中断循环,但enter键仍注册为字符: #inlcude <iostream> #include <conio.h> std::cout << "Password: "; char c; // Masks the password while ((c = _getch())) { if (c == '\n') { break; }

我试图屏蔽用户输入并将其存储在变量中。当我按enter键时,程序应中断循环,但enter键仍注册为字符:

#inlcude <iostream>
#include <conio.h>

std::cout << "Password: ";

char c;

// Masks the password
while ((c = _getch()))
{
    if (c == '\n')
    {
        break;
    }

    passwd.push_back(c); // put it onto the back of the password
    _putch('*'); // output a '*' character
}
#包括
#包括
std::cout在许多系统(包括Windows)上,换行“字符”(
'\n'
)实际上表示两个字符的组合:回车符(ASCII 13)加换行符(ASCII 10)

对于“回车”键(也就是回车键),使用
'\r'
转义序列:

#包括
#包括
#包括
int main()
{
std::字符串passwd;

std::不能改为检查
'\r'
。我听说\r在windows上使用,但与\n结合使用