-1
我有以下代碼:什麼導致這個奇怪的輸出?
void doPlayerMove(void)
{
bool moved = false;
while (!moved)
{
printf("\nWhere is the piece you want to move?(rc):");
int r = getchar() - '0';// gets the row number
int c = getchar() - '0';// gets the column number
printf("%d:%d", r, c);// prints the chosen row/column
clearInput();
printf("\nWhere is the space you want to move to?(rc):");
int r2 = getchar() - '0';
int c2 = getchar() - '0';
printf("%d:%d", r2, c2);
...
}
}
void clearInput(void)
{
while(getchar() != '\n');
}
這是輸出我得到:
Where is the piece you want to move?(rc):51
5:1
Where is the space you want to move to?(rc):40
4:00
怎麼了額外的0?有沒有人看到問題在哪裏?
來自下一個'printf'調用? :D – c00kiemon5ter
c00kie可能會出現問題;如果將'%d:%d'更改爲'%d:%d \ n',額外的零就會消失嗎? – sarnold
在第二次輸入後調用'clearInput();'。 – adatapost