2016-11-19 71 views
-2

那麼我只學了2個月的C程序設計。我在這裏得到的代碼是解決收銀系統問題。我得到了一些條形碼號碼,項目名稱和價格。收銀員輸入多個條形碼並用F結束後,應顯示具有項目編號,代碼和價格的收據。 這些是我編譯的代碼。但是,我只能輸入一個條碼,程序崩潰。C程序收銀系統

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

int main() 
{ 
typedef struct goods 
{ 
    char goods_code[6]; 
    char goods_descrip[20]; 
    float price; 
}goodtype; 

goodtype goods[13]; 
strcpy(goods[0].goods_descrip, "Chicken"); 
strcpy(goods[0].goods_code, "00310"); 
goods[0].price = 35.00; 

strcpy(goods[1].goods_descrip, "Pork"); 
strcpy(goods[1].goods_code, "00311"); 
goods[1].price = 20.50; 

strcpy(goods[2].goods_descrip, "Beef"); 
strcpy(goods[2].goods_code, "00322"); 
goods[2].price = 45.00; 

strcpy(goods[3].goods_descrip, "Fish"); 
strcpy(goods[3].goods_code, "00323"); 
goods[3].price = 40.00; 

strcpy(goods[4].goods_descrip, "Walmart T Shirt"); 
strcpy(goods[4].goods_code, "00510"); 
goods[4].price = 75.00; 

strcpy(goods[5].goods_descrip, "Walmart Trousers"); 
strcpy(goods[5].goods_code, "00511"); 
goods[5].price = 120.00; 

strcpy(goods[6].goods_descrip, "Walmart Coat"); 
strcpy(goods[6].goods_code, "00512"); 
goods[6].price = 100.00; 

strcpy(goods[7].goods_descrip, "Walmart Jumper"); 
strcpy(goods[7].goods_code, "00513"); 
goods[7].price = 85.00; 

strcpy(goods[8].goods_descrip, "Mug"); 
strcpy(goods[8].goods_code, "00710"); 
goods[8].price = 15.50; 

strcpy(goods[9].goods_descrip, "Fry Pan"); 
strcpy(goods[9].goods_code, "00711"); 
goods[9].price = 200.00; 

strcpy(goods[10].goods_descrip, "Bowl"); 
strcpy(goods[10].goods_code, "00712"); 
goods[10].price = 25.00; 

strcpy(goods[11].goods_descrip, "Dish"); 
strcpy(goods[11].goods_code, "00713"); 
goods[11].price = 25.00; 

char tempCode[6]; 
char receiptNM[20], receiptCD[6]; 
char stop[2] = {"F"}; 
float receiptPC, ttlcost = 0; 
unsigned int i; 

printf("Please enter the item code. Type F to finish"); 
scanf("%s", &tempCode); 

while (strcmp(tempCode, stop)){ 
    for (i = 0; i <= 12; ++i){ 
     if (strcmp(tempCode, goods[i].goods_code) == 0){ 
      strcpy(receiptNM, goods[i].goods_descrip); 
      strcpy(receiptCD, goods[i].goods_code); 
      receiptPC = goods[i].price; 
      ttlcost += goods[i].price; 
     } 
     else{ 
      printf("This item code does not exist! Try again!\n"); 
    } 
printf("Please enter the item code. Type F to finish"); 
scanf("%s", &tempCode); 
} 
printf("_____________________________________\n\n"); 
printf("  THANK YOU FOR VISITING US!  \n"); 
printf("_____________________________________\n"); 
printf("   Here is your receipt:  \n\n"); 
printf("%10s%20s%10s", "Item", "Code", "Price"); 
printf("%10s%20s%10.2f\n", receiptNM, receiptCD, receiptPC); 
printf("\n_____________________________________\n"); 
printf("   TOTAL COST:%.2f   \n", ttlcost); 
} 

我正在爲此奮鬥幾個小時,無法修復它。 示例輸出應該是Sample receipt

當我鍵入00310時,程序無法識別它。當我輸入310時,它可以。

此外,當代碼類型不存在時,錯誤消息應該只顯示一次。但在我的程序中,它有12次。

+0

'scanf(「%c」,&tempCode);'掃描單個字符,而不是字符串。也就是說,'tempCode'不會是NUL終止的字符串。所以你不能使用任何字符串函數。 – kaylum

+0

你的輸出是什麼?你期望解釋的是什麼? @ Sam.W – PassionInfinite

+1

另外,建議你學會使用調試器(如果你還沒有的話)。它會爲你節省很多時間。 – kaylum

回答

1

你已經做了幾個錯誤。讓我來指出它們: - Keine Lust指出的第一個問題:https://stackoverflow.com/a/40690102/3959321,其中你通過迭代到12(包含)來訪問內存超出數組邊界的內存。如果你聲明的12個元素的數組,該指數範圍從0到11,而不是0到12

for (i = 0; i <= 12; ++i){ //wrong iterations (< 12 should be here) 
    if (strcmp(tempCode, goods[i].goods_code) == 0) { 
     strcpy(receiptNM, goods[i].goods_descrip); 
     strcpy(receiptCD, goods[i].goods_code); 
     receiptPC = goods[i].price; 
     ttlcost += goods[i].price; 
    } 
    else { 
     printf("This item code does not exist! Try again!\n"); 
    } 
    printf("Please enter the item code. Type F to finish"); 
    scanf("%s", &tempCode); 
} 

你的程序肯定會打印出「此產品代碼不存在!......」 12次因爲else部分與if關聯並且嵌套在for循環中。 因此,每當找不到12次循環的項目時,它會被調用。

正確的程序是: -

---- //Rest above is same 
unsigned int i; 
printf("Please enter the item code. Type F to finish"); 
scanf("%s", tempCode); 

while (strcmp(tempCode, stop)) { 
    for (i = 0; i < 12; ++i){ 
     if (strcmp(tempCode, goods[i].goods_code) == 0){ 
      strcpy(receiptNM, goods[i].goods_descrip); 
      strcpy(receiptCD, goods[i].goods_code); 
      receiptPC = goods[i].price; 
      ttlcost += goods[i].price; 
      break; 
     } 
    } 
    if (i==12) //The loop iteration is complete, and i becomes 12 only when the for above hasn't been breaked (same as item found) 
     printf("This item code does not exist! Try again!\n"); 
    printf("Please enter the item code. Type F to finish"); 
    memset(tempCode,'\0',sizeof(tempCode)); 
    scanf("%s", tempCode); 
} 

您還最終收據部分犯了一個錯誤: -

printf("%10s%20s%10s", "Item", "Code", "Price"); 
printf("%10s%20s%10.2f\n", receiptNM, receiptCD, receiptPC); //receiptNM, receiptCD and receiptPC are only single character arrays. 

他們將只存儲一個不像你期望的字符串。爲了存儲所有這些數據,您可以創建一個結構數組(包含項目代碼,項目名稱和價格)。按照GumBoy的回答: - https://stackoverflow.com/a/40690211/3959321

+0

非常感謝:)現在我的程序運行完美。 –

1

這裏

for (i = 0; i <= 12; ++i){ 
    if (strcmp(tempCode, goods[i].goods_code) == 0){ 

您正在訪問未初始化的值在最後一次迭代(未定義的行爲),因爲你剛剛從goods[0]填充goods[11]

strcpy(goods[11].goods_descrip, "Dish"); 
strcpy(goods[11].goods_code, "00713"); 
goods[11].price = 25.00; 
0

看着你的輸出圖像。您將需要一個for循環來打印出值。在您當前的程序中,每當您的if語句中的條件得到滿足時,receiptCD,reciptNM和recieptPC的值將被替換。我認爲你需要爲你的收據添加另一個typedef結構。

我修復了一些問題,但你幾乎在那裏。最後一件事是文本的理由。

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

int main() 
{ 
typedef struct goods 
{ 
char goods_code[6]; 
char goods_descrip[20]; 
float price; 
}goodtype; 

goodtype goods[13]; 
strcpy(goods[0].goods_descrip, "Chicken"); 
strcpy(goods[0].goods_code, "00310"); 
goods[0].price = 35.00; 

strcpy(goods[1].goods_descrip, "Pork"); 
strcpy(goods[1].goods_code, "00311"); 
goods[1].price = 20.50; 

strcpy(goods[2].goods_descrip, "Beef"); 
strcpy(goods[2].goods_code, "00322"); 
goods[2].price = 45.00; 

strcpy(goods[3].goods_descrip, "Fish"); 
strcpy(goods[3].goods_code, "00323"); 
goods[3].price = 40.00; 

strcpy(goods[4].goods_descrip, "Walmart T Shirt"); 
strcpy(goods[4].goods_code, "00510"); 
goods[4].price = 75.00; 

strcpy(goods[5].goods_descrip, "Walmart Trousers"); 
strcpy(goods[5].goods_code, "00511"); 
goods[5].price = 120.00; 

strcpy(goods[6].goods_descrip, "Walmart Coat"); 
strcpy(goods[6].goods_code, "00512"); 
goods[6].price = 100.00; 

strcpy(goods[7].goods_descrip, "Walmart Jumper"); 
strcpy(goods[7].goods_code, "00513"); 
goods[7].price = 85.00; 

strcpy(goods[8].goods_descrip, "Mug"); 
strcpy(goods[8].goods_code, "00710"); 
goods[8].price = 15.50; 

strcpy(goods[9].goods_descrip, "Fry Pan"); 
strcpy(goods[9].goods_code, "00711"); 
goods[9].price = 200.00; 

strcpy(goods[10].goods_descrip, "Bowl"); 
strcpy(goods[10].goods_code, "00712"); 
goods[10].price = 25.00; 

strcpy(goods[11].goods_descrip, "Dish"); 
strcpy(goods[11].goods_code, "00713"); 
goods[11].price = 25.00; 

char tempCode[6]; 
typedef struct receipt 
{ 
char receiptNM[20], receiptCD[6]; 
float receiptPC; 
}receipttype; 
receipttype receipt[13]; 
char stop[2] = {"F"}; 
float ttlcost = 0; 
unsigned int i; 
unsigned int count = 0; 



while (strcmp(tempCode, stop)){ 
printf("Please enter the item code. Type F to finish: "); 
scanf("%s", tempCode); 
for (i = 0; i <= 12; ++i){ 
    if (strcmp(tempCode, goods[i].goods_code) == 0){ 
     strcpy(receipt[count].receiptNM, goods[i].goods_descrip); 
     strcpy(receipt[count].receiptCD, goods[i].goods_code); 
     receipt[count].receiptPC = goods[i].price; 
     ttlcost += goods[i].price; 
     count++; 
    } 

} 

} 
printf("_____________________________________\n\n"); 
printf("  THANK YOU FOR VISITING US!  \n"); 
printf("_____________________________________\n"); 
printf("   Here is your receipt:  \n\n"); 
printf("%10s%20s%10s", "Item", "Code", "Price\n"); 
for (int j= 0; j < count; ++j){ 
printf("%10s%20s%10.2f\n", receipt[j].receiptNM, receipt[j].receiptCD, receipt[j].receiptPC); 
} 
printf("\n_____________________________________\n"); 
printf("   TOTAL COST:%.2f   \n", ttlcost); 
} 
+0

它的工作原理!非常感謝! –

0

由於沒有人提到它,下面是填充結構數組的更好方法。

typedef struct 
{ 
    char *code; 
    char *description; 
    float price; 
}goodtype; 

static goodtype goods[] = 
{ 
    { "00310", "Chicken" , 35.00 }, 
    { "00311", "Pork" , 20.50 }, 
    { "00322", "Beef" , 45.00 }, 
    { "00323", "Fish" , 40.00 }, 
    { "00510", "T Shirt" , 75.00 }, 
    { "00511", "Trousers", 120.00 }, 
    { "00512", "Coat" , 100.00 }, 
    { "00513", "Jumper" , 85.00 }, 
    { "00710", "Mug"  , 15.50 }, 
    { "00711", "Fry Pan" , 200.00 }, 
    { "00712", "Bowl" , 25.00 }, 
    { "00713", "Dish" , 25.00 } 
}; 
static int goodscount = sizeof(goods)/sizeof(goods[0]); 

int main(void) 
{ 
    for (int i = 0; i < goodscount; i++) 
     printf("%3d %s %s\n", i, goods[i].code, goods[i].description); 
    printf("Number of items: %d\n", goodscount); 
} 

有幾個優勢,做這種方式:

  • 它需要更少的輸入,減少人爲錯誤的機會
  • 節省一點記憶,因爲你不需要去猜測陣列需要有多大的codedescription陣列。
  • 避免緩衝區溢出的可能性(例如,如果你猜錯了字符串的長度)。
  • 節省一點時間,因爲您不需要執行所有這些操作。
  • 自動計算數組中的條目數,這樣就不會出錯。
+1

我調整了我的程序,現在看起來更整齊:) –