检测整数类型变量上的空白输入

Detect blank input on integer type variable?

本文关键字:空白 输入 整数 类型变量 检测      更新时间:2023-10-16

我目前正在从事基于文本的冒险游戏作为课程的项目。我大部分时间都开始了,正常工作。唯一的问题是,当我询问用户他们想更改到哪个房间时,如果他们输入空白输入,则一条消息应输出,说"您必须选择一个房间"。对于我的一生,我无法弄清楚。非常感谢任何帮助。

代码:

#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;
}

我刚刚运行了您的代码,当您击中Enter时,它将不断等待,直到您输入一个数字或某些内容,例如字符或字符串。我确实发现,如果您从

更改代码
cin >> room_change;

to

cin >> noskipws >> room_change;

当用户输入空白时,它将导致cin.fail()返回true,然后继续打印"选择现有房间"。

在您的情况下,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"。

唯一的问题是当我询问用户他们想更改的用户时,如果他们输入空白输入,则一条消息应输出说"您必须选择一个房间。"

使用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. ";
}
#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;   
}

但是,一种简单得多的方法是使用字符串。如果这是分类的作业,并且您仅限于整数变量。如果您想检测缓冲区是否为空,则更为复杂。不管家庭作业的限制如何,OS层输入都是基于字符串的。如何使用CIN.Get()检测空用户输入?