2014-03-28 27 views
0

嗨,我剛開始編程在C和我在努力寫旨在充分整數的字符串,然後輸出,如果被檢查的值比其前一個小的程序。但我不能讓程序重複數據,它似乎只是檢查第一個值。我嘗試過使用循環,但這使我更加困惑。抱歉提出這樣一個基本問題。這是我到目前爲止的代碼:獲取程序不斷重複輸入用C

#include <stdio.h> 
#include <stdlib.h> 

int 
main(int argc, char *argv[]) { 

    int num; 
    int smaller=0; 

    printf("Input integers: "); 
    scanf("%d", &num); 

    if (num<smaller) { 
     printf("*** value %d is smaller than %d \n", num, smaller); 
    } 
    smaller=num; 

    return 0; 
} 
+2

嗯.. 。如果你想重複一些你可能需要的循環...你有沒有試過按照基本的C語言教程來學習像/ while循環這樣的基本語言結構? – vanza

+0

使用while循環。 – Phonon

回答

3

你可以使用一個do-while循環一遍又一遍地詢問用戶的值,直到他們鍵入內容無效,比如0

int smaller=0; 
int num=0; 
do { 
    printf("Input an integer (0 to stop): "); 
    scanf("%d", &num); 

    if (num<smaller) { 
     printf("*** value %d is smaller than %d \n", num, smaller); 
    } 
    smaller=num; 
} while (num != 0);