检测整数类型变量上的空白输入
Detect blank input on integer type variable?
我目前正在从事基于文本的冒险游戏作为课程的项目。我大部分时间都开始了,正常工作。唯一的问题是,当我询问用户他们想更改到哪个房间时,如果他们输入空白输入,则一条消息应输出,说"您必须选择一个房间"。对于我的一生,我无法弄清楚。非常感谢任何帮助。
代码:
#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()检测空用户输入?
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何使用用户输入在C++中正确填充2D数组
- C++MySQL C api用户输入行
- 无法通过空白将文本文件行分隔为矢量
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- 检测整数类型变量上的空白输入
- 输入层类型:图像数据在窗口咖啡cpp中给出空白输出
- 为什么从文件输入中读取 std::string 会返回空白
- C++,如何获得多个输入除以空白
- 如何使用getline()避免空白输入
- 如何使用空白作为分隔符对字符串进行标记并将标记输入到列表中