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

时间:2016-03-08 23:17:56

标签: c++ visual-studio-2015

试图阻止用户输入char。这段代码在我脑海中是有意义的。我创建的第一个if语句按预期工作(它阻止用户输入char)。但是当用户做出正确的选择时,开关会直接进入默认情况。在我输入错误处理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();
}

}

3 个答案:

答案 0 :(得分:1)

问题在于,当您确定错误后,再次调用您的函数:

Input();

当用户输入一个好的号码时,它会以良好的输入执行开关。然后它返回到调用者,在错误处理后重新开始,然后第二次执行带有未初始化a的开关

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

潜在纠正:

while ( (cin >> errorhandle) && (errorhandle < '0' || errorhandle > '9') )
    cout << "You have not entered a number try again! " << endl;
a = errorhandle-'0';
switch (a)
...

答案 1 :(得分:0)

在这一行:

a = (int)errorhandle;

您要将ascii char转换为int。 &#39; 1&#39;的价值与1.不同1.查看ascii table

在递归致电Input()之后,您将继续在未初始化的a声明中使用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');
}

答案 2 :(得分:0)

以前的答案会突显你发生的事情,你可以这样解决:

capture_table(USER, PWD, DB, QUERY, Functor) :-
    query(USER, PWD, DB, QUERY, _Columns, Rows),
    maplist(capture_table(Functor), Rows).
capture_table(Functor, Row) :-
    Clause =.. [Functor|Row],
    assertz(Clause).

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

并且您无法将char转换为类似的int。甚至不要转换为int只是比较switch语句中的char值。