2013-10-09 62 views
-1

嗯,我編碼這一些功課,我知道它可以更好地修改,並可以使用一些更多的意見,但這一個錯誤正在殺死我......想知道如果它只是我或某事,但你能告訴我,如果錯誤發生在你身上。爲什麼我的閱讀類型爲空?

錯誤:閱讀類型沒有任何數據。

(你可以使用這些投入,現在因爲我沒有錯誤檢查程序) 測試變量:

 
2345 
AA 
20 
30 
40 
50 
60 
10 
R(supposed to be entered) but program skip 

它是假設採取輸入rea_type但它並繼續跳到其他線路。

下面是代碼:

const float deposit = 1500.00; 
const unsigned multiplier =1; 
const float rate_One = 6.350; 
const float rate_Two = 14.520; 
unsigned bill_Cycle = 0, 
     no_Days = 0; 
float bi_Exch_Rate = 0, 
     ba_Exch_Rate = 0; 
float cur_Read=0, 
     prev_Read=0, 
     cur_Usage=0, 
     cur_Peri_Charg=0; 
char c_Digit[20], 
    premis_numb[20]; 
char rea_type; 
/**Information to be collected from the user**/ 
     system("cls"); 
     printf("\nPlease enter the customer digits: "); 
     scanf("%s",&c_Digit); 
     printf("\nPlease enter Premise Number:"); 
     scanf("%s",&premis_numb); 
     printf("\nPlease enter the Billing Cycle: "); 
     scanf("%d",&bill_Cycle); 
     printf("\nPlease enter the No. of Days: "); 
     scanf("%d",&no_Days); 
     printf("\nPlease enter the Billing Exchange Rate: "); 
     scanf("%f",&bi_Exch_Rate); 
     printf("\nPlease enter the Base Exchange Rate: "); 
     scanf("%f",&ba_Exch_Rate); 
     printf("\nPlease enter the Current Reading: "); 
     scanf("%f",&cur_Read); 
     printf("\nPlease enter the Previous Reading: "); 
     scanf("%f",&prev_Read); 
     printf("What is the Reading Type: "); 
     scanf("%c",&rea_type); 
     cur_Usage = cur_Read-prev_Read; 
     if (cur_Usage<100) 
     { 
      cur_Peri_Charg = cur_Usage*rate_One; 
     } 
     else 
     { 
      cur_Peri_Charg = (((cur_Usage-100) * rate_One)+(cur_Usage*rate_Two)); 
     } 
     strcat(premis_numb,c_Digit);/**Joins the Premis Number and the Customer digits together**/ 
/**Information to be displayed showing user all input and calculations.**/ 
    system("cls"); 
printf("\tStored constants for calculation of customer bill\n"); 
printf("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); 
printf("  Customer Number:%s",premis_numb); 
printf("   Current Usage:%.2f \n",cur_Usage); 
printf("\n"); 
printf("  Billing Cycle:%d\t\t  No of Days:%d\n",bill_Cycle,no_Days); 
printf("\n"); 
printf("  Billing Exchange Rate:%.2f  Base Exchange Rate:%.2f\n",bi_Exch_Rate,ba_Exch_Rate); 
printf("\n"); 
printf("  Deposit:%.2f\t\t  Multiplier:%d\n",deposit,multiplier); 
printf("\n"); 
printf("  Rate 1:%.3f\t\t  Rate 2:%.3f\n",rate_One,rate_Two); 
printf("\n"); 
printf("  Current Reading:%.2f\t  Previous Reading:%.2f\n",cur_Read,prev_Read); 
printf("\n"); 
printf("  Current Usage Reading:%.2f Reading Type:%c\n",cur_Peri_Charg,rea_type); 
printf("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); 

回答

1

考慮使用字符串rea_type因爲這將是更強大的壞輸入比單個字符,即改變:

char rea_type; 

到:

char rea_type[20]; 

和:

scanf("%c",&rea_type); 

到:

scanf("%s", rea_type); 



另外請注意,您應同時更改:

scanf("%s",&c_Digit); 

和:

scanf("%s",&premis_numb); 

到:

scanf("%s", c_Digit); 

和:

scanf("%s", premis_numb); 

你並不需要採取一個字符串的地址,因爲它已經是有效的指針。在這種情況下並不重要,但當字符串確實是指針(而不是數組)時,這是一個很好的習慣。

+1

由於'c_Digit'和'premis_numb'都是數組,所以在這種情況下無關緊要。 –

+0

@Paul R我把它留作字符,因爲它只需要一個字母或者假設只接受一個字母......並且我對premis和c_digit進行了更改,謝謝 – user2861799

+0

即使您只需要一個字符串字符,否則很容易得到一個空白字符而不是預期的字符。之後你可以解析字符串,並刪除任何空白或其他不需要的字符,並只提取你需要的字符。 –

0

您不需要傳遞char []的引用。 premin_num & c_Digit是char []的起始地址。做這樣的

scanf("%s",premis_numb); 
scanf("%s",c_Digit); 

一個可能的原因,你的編譯器在rea_type scanf函數跳過輸入是因爲有用戶輸入一個換行符,so if there's a stray newline in the input stream (from a previous entry, for example) the scanf call will consume it immediately & skip taking input.

解決方案:

之前提供空間%c

scanf(" %s",&rea_type); 
0

它不會跳過讀取字符,它會在最後一個scanf後面的輸入緩衝區中讀取換行符

這是簡單的修復,告訴scanf跳過前導空白:

scanf(" %c", &rea_type); 

通知的格式代碼前的空格。

+0

ahhh對不起測試變量沒有輸入正確的順序....只是意識到我改變了代碼順序 – user2861799

+0

@ user2861799更新了我的答案。 –

+0

感謝您的幫助 – user2861799