2016-03-08 116 views
-3

试图阻止用户输入字符。这段代码在我的脑海中是有道理的。我做的第一条if语句按预期工作(它可以防止用户输入字符)。但是,当用户做出正确选择时,交换机将直接进入默认情况。在我输入错误处理if语句之前,交换机正常工作。为帮助不确定为什么代码不起作用C++简单

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(); 
} 

}欢呼

+1

错误消息/正在发生的事情的解释将有所帮助。 –

+0

你显然必须重构你的代码以避免重复。另外,从编译器提供错误文本。 –

+0

您可能的意思是!=而不是==每个case语句后立即 – kvr

回答

1

的问题是,当你已经确定了错误,你再打电话你的函数:

Input(); 

当用户然后进入一个很好的号码,它以良好的输入执行开关。然后返回给调用者,错误处理后恢复,并执行第二次与未初始化a

开关还有另外一个问题:当你使用a = (int)errorhandle;,“1”的输入将输入到一个整数将被转换为'1'的ascii值而不是1。因此,您的案例值应该坚持所引用的值。

潜在的修正:

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

您的修正解决了我的问题,谢谢。我想感谢所有帮助过的人。我将不得不阅读ascii值。再次感谢 –

0

在这一行:

a = (int)errorhandle; 

你是一个ASCII char转换为int。 '1'的值与1不同。查看ascii table

同样在递归调用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'); 
} 
+0

哈哈,downvotes? –

0

以前的答案exaplain什么happeneing你能解决这个问题是这样的:

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值。

相关问题