2012-06-29 42 views
-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?有沒有人看到問題在哪裏?

+1

來自下一個'printf'調用? :D – c00kiemon5ter

+1

c00kie可能會出現問題;如果將'%d:%d'更改爲'%d:%d \ n',額外的零就會消失嗎? – sarnold

+0

在第二次輸入後調用'clearInput();'。 – adatapost

回答

0

由於OP說,在註釋:

問題解決了,它是從一些功能我是在 調用一些輸出...抱歉虛驚一場!

相關問題