不确定为什么代码不能C++简单
Unsure why code doesn't work C++ simple
试图阻止用户输入字符。这段代码在我的脑海中很有意义。我所做的第一个 if 语句按预期工作(它阻止用户输入字符)。但是,当用户做出正确的选择时,开关将直接进入默认情况。在我输入错误处理 if 语句之前,开关工作正常。为帮助干杯
void Input()
{
char errorhandle;
int a;
cout << "It's " << player << "'s turn Enter where you want your shape: ";
cin >> errorhandle;
if (errorhandle < '0' || errorhandle > '9')
{
cout << "You have not entered a number try again!" << endl;
Input();
}
else
{
a = (int)errorhandle;
}
switch (a)
{
case 1:
if (board[0][0] == '1')
{
board[0][0] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 2:
if (board[0][1] == '2')
{
board[0][1] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 3:
if (board[0][2] == '3')
{
board[0][2] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 4:
if (board[1][0] == '4')
{
board[1][0] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 5:
if (board[1][1] == '5')
{
board[1][1] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 6:
if (board[1][2] == '6')
{
board[1][2] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 7:
if (board[2][0] == '7')
{
board[2][0] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 8:
if (board[2][1] == '8')
{
board[2][1] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
case 9:
if (board[2][2] == '9')
{
board[2][2] = player;
}
else
{
cout << "The place is already in use, try again!" << endl;
Input();
};
break;
default:
cout << "You have entered an invalid option, try again" << endl;
Input();
}
}
问题是,当你发现错误时,你再次调用你的函数:
Input();
当用户输入一个正确的数字时,它会使用正确的输入执行开关。 然后,它返回到调用方,在错误处理后恢复,并第二次执行交换机,并显示未初始化的a
还有另一个问题:当您使用 a = (int)errorhandle;
将输入转换为整数时,输入 '1' 将被转换为 ascii 值 '1' 而不是 1。 因此,您的案例值应坚持引用的值。
潜在的修正:
while ( (cin >> errorhandle) && (errorhandle < '0' || errorhandle > '9') )
cout << "You have not entered a number try again! " << endl;
a = errorhandle-'0';
switch (a)
...
在这一行中:
a = (int)errorhandle;
您正在将 ASCII char
转换为int
。"1"的值与 1 不同。查看 ascii 表。
此外,在对Input()
进行递归调用之后,您将继续在未初始化的switch
语句中使用a
。
if (errorhandle < '0' || errorhandle > '9') {
cout << "You have not entered a number try again!" << endl;
Input();
return; // Stop execution after this line.
// This should be done in all cases of a call to input.
} else {
a = (int)(errorhandle - '0');
}
前面的答案解释了发生了什么,你可以这样修复它:
if (errorhandle < '0' || errorhandle > '9')
{
cout << "You have not entered a number try again!" << endl;
Input();
return; // < new | stops the function
}
在这种情况下,我会避免递归,但这也可以。
而且你不能像那样将字符转换为整数。甚至不要转换为 int,只需比较 switch 语句中的字符值即可。
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法