不确定为什么代码不能C++简单

Unsure why code doesn't work C++ simple

本文关键字:C++ 简单 不能 代码 为什么 不确定      更新时间:2023-10-16

试图阻止用户输入字符。这段代码在我的脑海中很有意义。我所做的第一个 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 语句中的字符值即可。