2011-02-24 90 views
0
/* 
Program to calculate trip and plan flights 
*/ 
#define TRIP 6 
#define DEST 1 
#include <stdio.h> 

int error_dest(int type_num, int cont_num, int dest_code, int check); 


int main(void) 
{ 
    int check, type_num, cont_num, index, i, dest_code, trip_num, row, col; 
    int travelint[TRIP][DEST], travelarea[TRIP]; 
    char area_code, S, M, L, N, P, K, R, C, U, W, O; 

    trip_num = 7; 
    while (trip_num > TRIP) 
    { 
    printf("Please enter the number of trips:"); 
    scanf("%d", &trip_num); 
    if (trip_num < TRIP) 
    { 
     printf("Valid trip number. Please proceed to enter destination code.\n"); 
    } 
    else 
    { 
     printf("Invalid trips. Please enter no more then 6 trips.\n"); 
    } 
    } 

    /*********************************************************************************/ 

    for (i=0; i < trip_num ; i++) /*destination code input*/ 
    { 
    printf("Please enter destination code:"); 
    scanf("%d", &dest_code);     /*input of destination code*/ 
    check = error_dest(type_num, cont_num, dest_code, check); 
    if (check == 2) 
    { travelint[i][0]=dest_code; } 
    else 
    { 
     while (check == 1) 
     { 
     printf("Please enter destination code:"); 
     scanf("%d", &dest_code);     /*input of destination code*/ 
     check = error_dest(type_num, cont_num, dest_code, check); 
     if (check == 2) 
     { travelint[i][0]=dest_code; } 

     } 
    } 
    printf("Please select from the following that best describes your destination:\n"); 
    printf("S Small city - population under 50,000\n"); 
    printf("M Medium city - population between 50,000 and 500,000\n"); 
    printf("L Large city - pop. over 500,000\n"); 
    printf("N Natural formation like a mountain, a lake, a cave, a geyser, a fjord, a canyon, etc.\n"); 
    printf("P Designated park or reserve such as a wildlife refuge, a national park, a bioreserve, or a protected marine area\n"); 
    printf("K Man made landmark like the Great Wall of China, the Taj Mahal, or Stonehenge\n"); 
    printf("R State or province or region of a country\n"); 
    printf("C Whole country\n"); 
    printf("U Multiple countries like traveling through Europe\n"); 
    printf("W Ocean voyage\n"); 
    printf("O Any other type of destination - such as visiting the sites of the seven wonders of the world\n"); 
    printf("Please enter the Area Letter code:"); 
    scanf("%c", &area_code);  

    } 
    /*******************************************************************************/ 

    /*print for destination_code*/ 

    for (row = 0; row < trip_num; row++) 
    { 
    for (col=0; col < DEST; col++) 
     printf("Trip[%d] = %d\n", row+1, travelint[row][col]); 
    } 

    return 0; 

} 

error_dest(type_num, cont_num, dest_code, check) 
{ 
    cont_num = dest_code/10000;    /*math for error check*/ 
    type_num = dest_code/1000 - cont_num*10; 

    if ((cont_num <= 7) && (cont_num > 0) && (type_num <= 5) && (type_num >=0)) 
    { /* loop for checking destination code*/ 
    check = 2 ; 
    return check; 
    } 
    else 
    { 
    printf("%d is a invalid code\n", dest_code); 
    check = 1; 
    return check; 
    } 
} 

出於某種奇怪的原因在scanf(「%c」,& area_code);它只是提前運行並打印dest_code數組而不讓我輸入任何字符,我不知道我到底做錯了什麼。掃描單個字符C

+0

不是你的問題的答案,但當trip_num == TRIP你說它是無效的,但然後接受它。 – user470379 2011-02-24 02:51:37

+2

'char area_code,S,M,L,N,P,K,R,C,U,W,O;'我看到11個變量,你根本沒有在你的代碼中使用。 – corsiKa 2011-02-24 02:53:50

+0

我不確定我看到了我接受的地方。 S M L N ...是從printf中選擇的選項,即用戶將要在scanf area_code和area_code中輸入的內容是我要存儲單個字符的地方 – 2011-02-24 02:54:15

回答

3

如果您只想抓住一個字符,也許最好使用getchar()而不是scanf()?

+0

頂部的所有type_num/cont_num/des_code /檢查唯一的問題是我們的教授是非常嚴格的沒有使用材料/功能,她沒有介紹這是唯一的原因,我試圖讓scanf()工作._。 – 2011-02-24 02:58:50

1

您可以在scanf後打印area_code,我想它可能是'\ n'這是您輸入的dest_code行的最後一個字符。

+0

我不確定你指的是什麼。 – 2011-02-24 02:58:15

+0

@Thao Nguyen使用printf(「%c」,area_code);看看什麼area_code是 – ZelluX 2011-02-24 04:12:50

0

您應該stdin讀取字符之前清空緩衝區:

int c = 0; 
while (c != '\n' && c != EOF) 
{ 
    c = getchar(); 
} 

那麼你可以使用scanf讀你的性格或getchar更換。

1

基本上發生了什麼事是這樣的:你打印「請輸入旅行次數」消息到屏幕上。用戶鍵入4,然後點擊回車鍵,這意味着stdin緩衝區看起來像這樣:「4 \ n」。然後使用「%d」格式字符串調用scanfscanf查看stdin緩衝區,並看到4.它看着下一個字符,換行符,並且看到它不是數字的一部分(如%d指定),所以它完成了格式化字符串的完成並離開了文件指針在換行符處。它將字符'4'轉換爲整數4並將其放入trip_num並返回。

下次打電話給scanf時,它會在換行符停留的地方繼續。這次的格式字符串是「%c」,所以它只是從當前換行符(「\ n」)的緩衝區中抓取下一個字符,並將其放入dest_code並返回。如果您希望scanf函數在此情況下跳過空格,則必須通過在第二個scanf(目標代碼)的「%c」格式之前添加一個空格來明確告訴它。然後scanf將跳過所有空格(包括該換行符),直到它遇到一個非空白字符,它放在dest_code中。

TL; DR:將第二個scanf調用更改爲scanf(" %c", &dest_code)。並修復其他人指出的其他錯誤,以避免其他錯誤出現。

+0

是的,我想我明白你要去哪裏 – 2011-02-24 03:40:37

0

這可能有幫助,但前面說過,您可能需要將getchar()放入while循環中。您可能還需要fgets從鍵盤抓取stdin。

而(1){

printf("Enter Message Type:"); 
fflush(stdout) ; 

// scan msg.hdr from received message. 
scanf("%d", &(msg.m_hdr)); 
while(getchar() != '\n'){} 

printf("Enter your Message:"); 
fflush(stdout); 

// grab data from keyboard 
fgets(msg.m_data, sizeof(msg.m_data), stdin); 
0

使用 「fflush(標準輸入)」 你 「的printf」 語句的字符之前輸入的字符,即之前。 它將清除輸入緩衝區,因此您可以掃描所需的字符。或者直接在「%c」命令之前給出空間。像---------- scanf(「%c」,& area_code); ---------------