【问题标题】:Detect blank input on integer type variable?检测整数类型变量的空白输入?
【发布时间】:2019-07-24 18:58:10
【问题描述】:

我目前正在开发一个基于文本的冒险游戏,作为课堂项目。我几乎一切都开始了并且工作正常。唯一的问题是,当我问用户他们想换到哪个房间时,如果他们输入一个空白输入,那么应该输出一条消息说“你必须选择一个房间”。对于我的生活,我无法弄清楚。非常感谢任何帮助。

代码:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {

bool game_play = true;
bool game_start = true;
int room_change;
int room_current = 0;

while (game_play == true) {
    if (game_start == true) {
        srand((unsigned int)time(NULL));
        room_change = rand() % 2 + 1;
        game_start = false;
    }
    else {
        for (bool check = false; check == false;) { // Check if input is invalid
            cin >> room_change;
            if (cin.fail()) {
                cout << "Choose an existing room.";
                cin.clear();
                cin.ignore();
            }
            else if (room_change == room_current) {
                cout << "You're already in that room.";
            }
            else {
                check = true;
            }
        }
    }
    switch (room_change) {
    case 1:
        cout << "You are in room 1.";
        room_current = 1;
        break;
    case 2:
        cout << "You are in room 2.";
        room_current = 2;
        break;
    case 3:
        game_play = false;
        break;
    default:
        cout << "That room doesn't exist.";
    }
}
return 0;
}

【问题讨论】:

标签: c++ if-statement input integer


【解决方案1】:

唯一的问题是当我问用户他们想换到哪个房间时,如果他们输入一个空白输入,那么应该输出一条消息说“你必须选择一个房间。”

使用std::getline,然后使用std::istringstream 从行中提取数字是一种更好的策略。

std::string line;
std::cout << "Choose an existing room. ";
while ( std::getline(std::cin, line) )
{
   // Try to get the room_change using istringstream.
   std::istringstream str(line);
   if ( str >> room_change )
   {
      // Successfully read the room.
      break;
   }

   // Problem reading room_change.
   // Try again.
   std::cout << "Choose an existing room. ";
}

【讨论】:

    【解决方案2】:

    我刚刚运行了你的代码,当你按下回车键时,它会一直等到你输入一个数字或一些无效的东西,如字符或字符串。我确实发现如果您将代码更改为

    cin >> room_change;
    

    cin >> noskipws >> room_change;
    

    当用户输入空白时,会导致 cin.fail() 返回 true,然后继续打印“Choose an existing room”。

    在您的情况下,while 循环将一直被调用,直到我们有有效的输入。 “选择现有房间”确实会重复,因为 room_change 是一个整数,所以当我们按 Enter 时,'\n' 将留在缓冲区中。下一次迭代的 while 循环会读取 '\n' 并执行 cin.fail() ,然后再让您输入其他内容。我发现的一种解决方案是使用更多的 cin.ignore() 语句。

    for (bool check = false; check == false;) { // Check if input is invalid
        cin >> noskipws >> room_change;
        if (cin.fail()) {
            cout << "Choose an existing room.";
            cin.clear();
            cin.ignore();
        } else if (room_change == room_current) {
            cout << "You're already in that room.";
            cin.ignore();
        } else {
            check = true;
            cin.ignore();
        }
    }
    

    原因是因为我们想去掉那个 '\n' 以便 cin.fail() 不执行。但是,我确实发现当您输入一个字符时,它会打印两次“选择现有房间”。它将第一次打印,因为字符不是整数,第二次因为那个 '\n'。

    【讨论】:

    • 妈的,连我都不知道。
    • 这是个好主意,但是我在使用它时遇到的问题是,即使输入数字,它仍然会触发 cin.fail()。
    • 在你的场景中,因为 room_change 是一个整数,我假设 '\n' 将保留在缓冲区中,所以调用 cin.fail() 来读取那个 '\n'。解决方法是添加更多 cin.ignore() 语句。我将编辑代码供您查看。
    【解决方案3】:
    #include <iostream>
    
    using namespace std;
    
    int main(){
        int room_change=200;
    
        cout<<"Enter Blank";
        cin>>room_change;
    
        if(room_change==NULL){
           cout<<"There is NO-THING"<<endl; 
        }
    
        if(room_change!=NULL){
           cout<<"There is something and that is :"<<room_change<<endl; 
        }
    
    
    return 0;   
    }
    

    但更简单的方法是使用字符串。如果这是一个家庭作业,并且您仅限于整数变量。如果要检测 Buffer 是否为空,则要复杂得多。无论作业限制如何,操作系统层输入都是基于字符串的。 How can I use cin.get() to detect an empty user input?

    【讨论】:

    • 如果你认为room_change==NULL 在用户输入一个空白行的情况下会计算为true,那你就错了。将room_change 初始化为一个不寻常的数字,比如 200,然后尝试一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多