Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 输入字符时忽略回车键_C++_Validation - Fatal编程技术网

C++ 输入字符时忽略回车键

C++ 输入字符时忽略回车键,c++,validation,C++,Validation,我试图验证一个简单的字符输入,但是如果用户在没有输入字符的情况下按enter键,它只会在控制台中打印一个空行,直到输入字符为止。我希望接受enter键作为无效输入,并继续验证,而不是只转到空行 cout << "Enter room type. (S)tandard or (P)remium: "; char roomType; cin >> roomType; while (cin.fail() || roomType != 's' && roomTyp

我试图验证一个简单的字符输入,但是如果用户在没有输入字符的情况下按enter键,它只会在控制台中打印一个空行,直到输入字符为止。我希望接受enter键作为无效输入,并继续验证,而不是只转到空行

cout << "Enter room type. (S)tandard or (P)remium: ";
char roomType;
cin >> roomType;
while (cin.fail() || roomType != 's' && roomType != 'S' && roomType != 'p' && roomType != 'P')
{
    cin.clear();
    cin.ignore(80, '\n');
    cout << "Error. Enter room type again: ";
    cin >> roomType;
}
cin.ignore(80, '\n');
cin.clear();
return roomType;
cout>roomType;
而(cin.fail()| | roomType!=“s”&&roomType!=“s”&&roomType!=“p”&&roomType!=“p”)
{
cin.clear();
cin.忽略(80,“\n”);
房间型;
}
cin.忽略(80,“\n”);
cin.clear();
返回房间类型;

****再次编辑了我的答案

我找到了一个解决方案,解释了为什么输入换行符时不输出错误消息。显然,这一行:

    cin.ignore(80, '\n');
这是问题的根源。一方面,while循环中的条件表示应该将换行符视为冲突并打印错误消息。然而,另一方面,上面的代码告诉编译器忽略换行符,甚至不检查它是否违反while循环的条件。您必须删除这一行,以便编译器甚至可以接受换行符,以检查它是否违反条件,或用于任何其他目的。我已更新了以下代码:

    // Example program
    #include <iostream>
    #include <conio.h>

    using namespace std;

    int main()
    {
        cout << "Enter room type. (S)tandard or (P)remium: ";
        char roomType;
        roomType = _getch();
        while (cin.fail() || (roomType != 's' && roomType != 'S' && roomType != 'p' && roomType != 'P') || (roomType == '\n'))
        {
            //cin.clear();
            cout << "\nError. Enter room type again: ";
            roomType = _getch();
            cout << roomType;
        }
        //cin.clear();
        cout << "\n\nSuccess" << endl;
        return 0;
    }
//示例程序
#包括
#包括
使用名称空间std;
int main()
{

无法再次编辑我的答案

我找到了解决方案,解释了为什么输入换行符时不输出错误消息。显然,这一行:

    cin.ignore(80, '\n');
是问题的根源。一方面,while循环中的条件表示应该将换行符视为冲突并打印错误消息。但是,另一方面,上面的行告诉编译器忽略换行符,而不检查它是否违反while循环或n的条件ot.您必须删除这一行,以便编译器甚至可以接受换行符,以检查它是否违反条件,或用于任何其他目的。我已更新了以下代码:

    // Example program
    #include <iostream>
    #include <conio.h>

    using namespace std;

    int main()
    {
        cout << "Enter room type. (S)tandard or (P)remium: ";
        char roomType;
        roomType = _getch();
        while (cin.fail() || (roomType != 's' && roomType != 'S' && roomType != 'p' && roomType != 'P') || (roomType == '\n'))
        {
            //cin.clear();
            cout << "\nError. Enter room type again: ";
            roomType = _getch();
            cout << roomType;
        }
        //cin.clear();
        cout << "\n\nSuccess" << endl;
        return 0;
    }
//示例程序
#包括
#包括
使用名称空间std;
int main()
{

当你可以使用更容易理解的
'\n'
时,为什么只打一个
10
?这将省去你的解释。谢谢你的回答。它不是roomtype==10吗?因为如果它相等,将触发验证?编辑:测试两者,控制台仍然输出空行而不是空行一条错误消息。你说得对,@KeeganHeffernan,这是我发布第一个解决方案时想到的。但是,它们的意思都是相同的,因为10是ascii表中“\n”的ascii值。因此在这种情况下,它们可以互换使用。@NewbietoProgramming我尝试了这两种解决方案,结果仍然是空行。如果你点击“输入”按钮两次,它输出无效消息并继续要求再次输入,@KeeganHeffernan。另外,你用我上面更新的代码试过了吗?如果你能使用更容易理解的
“\n”
,为什么只需打一个
10
?这样你就不用解释了。谢谢你的回答。这不是房间型的吗==10?因为如果它相等,则会触发验证?编辑:测试两者,控制台仍然输出空行而不是错误消息。你说得对,@KeeganHeffernan,这是我发布第一个解决方案时想到的。但是,它们都表示10是t中“\n”的ascii值他创建了ascii表。因此,在这种情况下,它们可以互换使用。@NewbietoProgramming我尝试了这两种解决方案,但结果仍然是空行。如果您按两次enter按钮,它将输出无效消息,并继续要求再次输入@KeeganHeffernan。另外,您是否用我上面更新的代码进行了尝试?我已更新了我的答案下面是@KeeganHeffernan。请检查它是否对你有效(对我有效)。我已经更新了下面的@KeeganHeffernan。请检查它是否对你有效(对我有效)。