2010-11-08 47 views
0

我有一個簡單的功課......做一個C程序,需要3個值3倍奇怪的事情用一個簡單的程序,可能scanf的問題

  1. 食品(焦炭)的ID
  2. CAL值食品(浮點)
  3. 量食品(浮點)的

下面是代碼和輸出程序給出我:

#include <stdio.h> 
#define NUM_OF_FOOD 3 

int main() 
{ 
    float food_cal[NUM_OF_FOOD]; 
    float food_amount[NUM_OF_FOOD]; 
    char food_id[NUM_OF_FOOD]; 
    int i; 
    float total_cal=0; 
    int most_fat_food_id=0; 
    printf("enter the following data: food ID, Cal value and Amount eaten \nexample A 10 3\n"); 
    for (i=0;i<NUM_OF_FOOD;i++) 
    { 
     printf("\nEnter product #%d:",i); 
     int inputLength = scanf("%c %f %f",&food_id[i],&food_cal[i],&food_amount[i]); 
     if (inputLength < 3) { 
      printf("input error, input length was %d excpexted 3", inputLength); 
      break; 
     } 
     if (!((food_id[i]>96 && food_id[i]<123) || (food_id[i]>64 && food_id[i]<91))) { 
      printf("ID input error"); 
      break; 
     } 
     if (food_cal[i] < 0) { 
      printf("Food Cal input error"); 
      break; 
     } 
     if (food_amount[i] < 0) { 
      printf("Food Amount input error"); 
      break; 
     } 
     printf("\n%c %5.2f %5.2f",food_id[i],food_cal[i],food_amount[i]); 
    } 
    for (i=0;i<NUM_OF_FOOD;i++) 
     total_cal+=food_cal[i]*food_amount[i]; 

    printf ("\nTotal amount of calories is %5.2f\n",total_cal); 
    for (i=1;i<NUM_OF_FOOD;i++) 
     most_fat_food_id = (food_cal[most_fat_food_id]<food_cal[i]) ? i : most_fat_food_id; 

    printf ("\nThe most fattening product is: %c with %5.2f calories",food_id[most_fat_food_id],food_cal[most_fat_food_id]); 

    return 0; 
} 

/* 
enter the following data: food ID, Cal value and Amount eaten 
example A 10 3 

Enter product #0:A 1000 2 

A 1000.00 2.00 
Enter product #1:B 500 3 
input error, input length was 1 excpexted 3 
Total amount of calories is 2000.00 

The most fattening product is: A with 1000.00 calories 
*/ 

它只是跳過第三

不知道爲什麼......兩個人看了看號碼的輸入,看起來不錯,但還是有一個錯誤。

+0

@Roger - 認錯 - 但是,請注意,在OP發佈了運行的例子,完美展示了自己的錯誤。 – KevinDTimm 2010-11-08 21:29:23

+0

@Kevin:是的,我有,不幸的是,在使用一種形式般的消息,一般建議爲這 - 環節是users5 ..的後來的問題和別人的問題很有用。 – 2010-11-08 21:30:37

+0

@Roger - 我會改變,如果你願意:) – KevinDTimm 2010-11-08 21:31:48

回答

1

輸入:甲1000 2 [ENTER]乙500 3 [ENTER] ...

第一scanf("%c %f %f")讀取 'A',1000,和2以及在[ENTER]

第二停止scanf("%c %f %f")讀取[ENTER]和扼流圈B與「%f」轉換。

您可以嘗試在讀取char之前使用scanf(" %c %f %f")強制跳過可選空白。

+0

thx ...這個竅門 – Iakovl 2010-11-08 21:30:40

+0

只需要記住一件重要的事情:「正常」的'scanf'轉換說明符(%d,%i,%f,%s)跳過前導空格(空格,換行符...); 「%c」沒有。要強制跳過空格,請在格式字符串中使用空格。 – pmg 2010-11-08 21:51:11

0

你不消耗 '\ n'

快速/骯髒的方法是添加一個變量:您scanf()

char readNewLine; 

,再添:

scanf("%c", &readNewLine); 

這很醜陋,但它已經足夠了 - 在使用C一段時間後,更好的方法將變得可用。

+0

更好的方法已經可用了。爲什麼不建議OP閱讀'fgets()',特別是因爲他的輸入數據會很小? – 2010-11-08 21:24:49

+0

因爲作爲功課,我不預設他/她的學習水平。如果教授(et.al.)沒有得到那麼多,這是太多的信息。我幾乎感到慚愧,我回答了這個問題(除了只是說'\ n'沒有被消耗) – KevinDTimm 2010-11-08 21:26:37

0

線33加後:

char nl = 0; 
scanf("%c", &nl); 

,這將消耗多數民衆贊成擰事情最終換行符。

上述建議後,這是輸出:

./test 
enter the following data: food ID, Cal value and Amount eaten 
example A 10 3 

Enter product #0:a 10 2 

a 10.00 2.00 
Enter product #1:b 3 29 

b 3.00 29.00 
Enter product #2:c 32 4 

c 32.00 4.00 
Total amount of calories is 235.00 

The most fattening product is: c with 32.00 calories 
+0

爲什麼你的程序名稱中有這麼多字符;) – KevinDTimm 2010-11-08 21:27:50