我的問題是,此函數返回的猜測是97和y的總和,而不是像switch語句指示的那樣只是字母。奇怪的是,當我剛回來時,它的工作方式和我預期的完全一樣。例如,當y = 4,並且返回y時,將顯示4。當y = 4並且返回'guess'時,顯示101。免責聲明:這不是完整的代碼。這真的很長,我認爲是多餘的,因爲我唯一的問題是這個特定的函數返回'猜測',甚至不會返回y。如果所有的代碼都可以使用,我會很樂意加以解決。C++ switch語句返回奇怪值
int letterguess(int z){
char guess;
int y;
y=letters[0][z];
switch (y){
case 0:
guess='a';
break;
case 1:
guess='b';
break;
case 2:
guess='c';
break;
case 3:
guess='d';
break;
case 4:
guess='e';
break;
case 5:
guess='f';
break;
case 6:
guess='g';
break;
case 7:
guess='h';
break;
case 8:
guess='i';
break;
case 9:
guess='j';
break;
case 10:
guess='k';
break;
case 11:
guess='l';
break;
case 12:
guess='m';
break;
case 13:
guess='n';
break;
case 14:
guess='o';
break;
case 15:
guess='p';
break;
case 16:
guess='q';
break;
case 17:
guess='r';
break;
case 18:
guess='s';
break;
case 19:
guess='t';
break;
case 20:
guess='u';
break;
case 21:
guess='v';
break;
case 22:
guess='w';
break;
case 23:
guess='y';
break;
case 24:
guess='y';
break;
case 25:
guess='z';
break;
}
return guess;
}
我真的不明白這可能會如何搞砸這樣。提前致謝。
爲什麼從返回'int'的函數返回'char'? – Griwes
'guess = static_cast(y)+'a';'替換您的開關。 –
David
@Dave:的確,雖然技術上不可移植。 –