2016-04-28 38 views
1

我已經寫了一個小代碼來從華氏溫度到攝氏溫度值。我想繼續輸入數據,直到按下'y'以外的任何其他鍵。但是這個循環不能以這種方式工作,並在一次迭代後停止。scanf不能正常工作在這個c程序

#include <stdio.h> 

int main() 
{ 
char ch='y'; 
int far, cen; 
do { 
    printf("again\n"); 
    scanf("%d",&far); 
    //cen = (5.0/9.0)*(far-32);//integer division will truncate to zero so we can make 5/9 to 5.0/9.0 
    cen = (5*(far-32))/9;//or this way we can use this formula 
    printf("\n%d\t%d",far, cen); 
    printf("ch=%c",ch); 
    scanf("%c",&ch); 
    }while(ch == 'y'); 

return 0; 
} 

這裏有什麼問題? P.S 我加一條線,犯了這樣的

#include <stdio.h> 

int main() 
{ 
char ch='y'; 
int far, cen; 
do { 
    printf("again\n"); 
    scanf("%d",&far);//here we press carriage return. this value is in stdin 
    //cen = (5.0/9.0)*(far-32);//integer division will truncate to zero so we can make 5/9 to 5.0/9.0 
    cen = (5*(far-32))/9;//or this way we can use this formula 
    printf("\n%d\t%d",far, cen); 
    scanf("%c",&ch);//putting a space before %c makes the newline to be consumed and now it will work well 
    if((ch == '\r')|| (ch == '\n')) 
     printf("1\n"); 
    printf("ch=%c",ch);//this takes the carriage return in stdin buffer 
    }while(ch == 'y'); 

return 0; 
} 

我需要知道這裏回車新的代碼爲\ R或\ n嗎?

如果Y後按「空格」或「返回」
+1

回車/換行 –

+0

在賦予'far'值後按'enter'。然後我打印'ch'中的值。 ch是'y',那麼這是一個運輸問題。 – mrigendra

+0

@mrigendra您可以在'scanf'之前輸出'ch' *的值。在* scanf之後嘗試打印*。 –

回答

-2

這是你會在%C

3

找到字符填寫完畢後scanf("%d",&far);值,然後按回車,scanf函數存儲在緩衝區中的回車。當它遇到代碼scanf("%c",&ch);中的第二個scanf時,它將回車中的回車作爲'ch'的輸入。所以它不會等待用戶輸入。

請看看後here

如在回答解決的辦法是把一個空間在scanf的一個指示

scanf(" %c",&ch); 
2

你應該總是檢查scanf返回值。如果用戶沒有輸入有效整數,那麼您的第一次使用scanf可能會失敗,在這種情況下,您正在使用far而未初始化它(這是未定義的行爲)。 scanf返回已成功掃描的項目數。如果您要求scanf掃描一個整數,那麼它應該返回1,如果它成功設法掃描一個整數。

int scanresult = scanf("%d", &far); 
if (scanresult != 1) 
{ 
    puts("Invalid input or unexpected end of input"); 
    return 1; 
} 

此外,%c轉換說明是獨特的,因爲它不會引起scanf吞噬不像其他轉換說明前述任一空格。要強制scanf吞噬空白(例如換行,回車,空格,製表符等),只需在%c之前放置一個空格字符,例如,

scanresult = scanf(" %c", &ch); 

scanf,空格字符實際上是解析,跳過所有空格一個指令。

0

這是因爲之前的換行符保留在緩衝區中。你可以簡單地通過這條線替換scanf

while((ch = getchar()) == '\n'); 

您將與ungetc()將需要結合相同的技術在很多場合。

+0

回車='\ r'或'\ n'? – mrigendra

+0

@mrigendra'\ r'在Windows的情況下,'\ n'在類Unix的情況下。 –

0

在scanf(「%c」,& ch)之上添加fflush()函數。因爲CONSOLE INPUT的緩衝區存儲了未返回到程序的字符。在以前的scanf中按下ENTER鍵:

#include <stdio.h> 

int main() { 
    char ch='y'; 
    int far, cen; 
do { 
    printf("again\n"); 
    scanf("%d",&far); 
    //cen = (5.0/9.0)*(far-32);//integer division will truncate to zero  so we can make 5/9 to 5.0/9.0 
    cen = (5*(far-32))/9;//or this way we can use this formula 
    printf("\n%d\t%d",far, cen); 
    printf("ch=%c",ch); 

    scanf("%c",&ch); // This scanf will be ignored, because loads last 
         // character from buffer that can be recognized 
         // by scanf which is pressed "ENTER" from previous scanf 
    printf("%d", ch)  // Shows 10, which is ASCII code of newline 
    fflush(stdin);  // Clear buffer 
    scanf("%c",&ch);  // Now it will prompt you to type your character. 

    // printf("%c"ch); //Without fflush, it must show 10, which is \n code  
    }while(ch == 'y'); 

    return 0; 
    } 
+0

如果你可以解釋爲什麼addind fflush()解決了這個問題,那麼這個答案會很棒...... –

+0

'fflush(stdin);'是未定義的行爲(UB)。 「Ifstream指向輸出流...... fflush函數會導致該流的任何未寫入數據被傳遞到主機環境以寫入該文件;否則,行爲未定義。」 C11§7.21.5.22由於'stdin'是一個輸入流,這就是UB – chux