不确定为什么代码不能工作C++

不确定为什么代码不能工作C++,c++,visual-studio-2015,C++,Visual Studio 2015,试图阻止用户输入字符。这个代码在我的头脑中是有道理的。我所做的第一个if语句按预期工作,它阻止用户输入字符。但是当用户做出正确的选择时,开关会直接切换到默认情况。在我输入错误处理if语句之前,开关工作正常。为帮助干杯 void Input() { char errorhandle; int a; cout << "It's " << player << "'s turn Enter where you want your shape: "; cin >&

试图阻止用户输入字符。这个代码在我的头脑中是有道理的。我所做的第一个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=interrorhandle;,将输入转换为整数时;,输入“1”将转换为ascii值“1”,而不是转换为1。因此,您的案例值应该坚持引用的值

潜在修正:

在这方面:

a = (int)errorhandle;
您正在将ascii字符转换为整数。“1”的值与1不同。看这张照片

同样,在递归调用Input之后,您将继续在未初始化的switch语句中使用

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语句中的char值。

错误消息/对发生的情况的解释会有所帮助。显然,您必须重构代码以避免重复。另外,请提供编译器中的错误文本。您的意思可能是!=而不是在每个case语句之后立即==顺便说一下,通过一点数学运算,您可以删除switch语句。行=数字/3;列=编号%3;。你的纠正解决了我的问题,谢谢。我要感谢所有帮助过我的人。我必须仔细阅读ascii值。再次感谢
if (errorhandle < '0' || errorhandle > '9')
{
    cout << "You have not entered a number try again!" << endl;
    Input();
    return; // < new | stops the function
}