繁体   English   中英

不确定为什么代码不起作用 C++ simple

[英]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 第二次执行 switch

还有另一个问题:当您使用a = (int)errorhandle;将输入转换为整数时a = (int)errorhandle; , '1' 的输入将被转换为 '1' 的 ascii 值而不是 1。所以你的 case 值应该坚持引用的值。

潜在修正:

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
}

在这种情况下我会避免递归,但这也有效。

并且您不能像那样将 char 转换为 int。 甚至不要转换为 int 只是比较 switch 语句中的 char 值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM