2016-12-28 60 views
-1

我想用C語言製作收銀程序。我用記錄結構,但是當我爲條形碼輸入輸入1時,它不顯示項目1;代替它顯示項2.這是我的代碼:爲什麼不按照記錄顯示輸出?

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

struct item 
{ 
    char name[10]; 
    int price; 
    int barcode; 
}; 
struct item detail[10] = { 
     "item1", 10, 1, 
     "item2", 20, 2, 
     "item3", 30, 3, 
     "item4", 40, 4, 
     "item1", 50, 5, 
     "item2", 60, 6, 
     "item3", 70, 7, 
     "item4", 80, 8, 
     "item3", 90, 9, 
     "item4", 100, 10 
}; 
int main() 
{ 
    int ibarcode[10]; 
    int qty[10]; 
    int tot[10]; 
    int j, i, k, grand; 
    char a; 
    printf("Program Kasir\n"); 
    for (j = 0; j < 10; j++) 
    { 
     printf("ebter barcode : "); 
     scanf("%d", &ibarcode[j]); 
     for (i = 0; i < 10; i++) 
     { 
      if (ibarcode[j] == detail[i].barcode) 
      { 
       printf("item : %s\n", detail[i].name); 
       printf("price : %d\n", detail[i].price); 
       printf("enter quantity : "); 
       scanf("%d", &qty[j]); 
       tot[j] = detail[j].price * qty[j]; 
      } 
      if (ibarcode[j] > 10) 
      { 
       printf("Barcode isn't valid'\n"); 
       j--; 
       break; 
      } 
     } 
     printf("\nbuy again? [Y/N] = "); 
     scanf("%s", &a); 
     if (a == 'Y' || a == 'y') 
     { 
      continue; 
     } else 
     { 
      break; 
     } 
    } 
    grand = 0; 
    system("cls"); 
    printf("\n name Kasir = Addzifi Moch G\n"); 
    printf(" Tanggal = 03 januari 2017\n"); 
    printf(" Jam  = 14:05 WIB\n\n"); 
    printf("+-------------------------------------------------------------------------------------------------+\n"); 
    printf("| Barcode | item \t\t\t| price  \t\t| quantity \t| Total |\n"); 
    printf("+-------------------------------------------------------------------------------------------------+\n"); 
    for (k = 0; k <= j; k++) 
    { 
     grand += tot[k]; 
     printf("| %d \t | %s\t    | %d\t\t  | %d\t\t\t| %d |\n", ibarcode[k], detail[k].name, detail[k].price, qty[k], tot[k]); 
    } 
    printf("+-------------------------------------------------------------------------------------------------+\n"); 
    printf("|\t\t\t\t\t\t\t Total Yang Harus Dibayarkan = %d |\n", grand); 
    printf("+-------------------------------------------------------------------------------------------------+\n"); 
} 
+0

使用'scanf函數( 「%S」,&a); '''char a;'是災難的祕訣,你告訴'scanf()'有足夠的空間在一個'char'中至少有兩個字符(一個字母和一個空字節來終止字符串) - 並且沒有,你應該可以使用'scanf('%c',&a);''''''''之前的空格是至關重要的 - 它會跳過流浪的換行符和其他空格。 –

+0

當你選擇條形碼0時,你的代碼並不是很高興。 –

回答

0

在C/C++索引從0開始而不是1

ibarcode [10]意味着有索引從0到9

+0

它仍然不能工作bro – daniel

+0

@AddzifiMochGumelar它是關於你的代碼的一般評論。順便說一句,我們不是'兄弟'。 – paweldac

+0

他沒有使用輸入作爲數組索引,他將它與'detail [i] .barcode'進行比較。 – Barmar

0

問題在於打印收據時,您沒有使用detail中的正確索引。您正在打印detail[k]的字段,但k不是客戶購買的物料的索引,它只是當前循環的for()循環。

您需要保存在第一個循環中搜索detail時發現的索引i以獲取價格。

取代大量單獨的數組,最好有另一個包含購買細節的結構。它可以使用指針來引用details數組中的項目。

struct purchase { 
    struct item *item; 
    int qty; 
    int tot; 
} items[10]; 

然後你的第一個循環會看起來像:

for (j = 0; j < 10; j++) { 
    int barcode; 
    scanf("%d", &barcode); 
    int item_found = 0; 
    for (i = 0; i < 10; i++) 
    { 
     if (barcode == detail[i].barcode) 
     { 
      int qty; 
      printf("item : %s\n", detail[i].name); 
      printf("price : %d\n", detail[i].price); 
      printf("enter quantity : "); 
      scanf("%d", qty); 
      items[j].qty = qty; 
      items[j].tot = qty * deatail[i].price; 
      items[j].item = &detail[i]; 
      item_found = 1; 
      break; 
     } 
    } 
    if (!item_found) { 
    { 
     printf("Barcode isn't valid'\n"); 
     j--; 
     break; 
    } 
} 

然後你就可以訪問打印收據時,詳細信息:

for (k = 0; k <= j; k++) 
{ 
    grand += tot[k]; 
    printf("| %d \t | %s\t    | %d\t\t  | %d\t\t\t| %d |\n", item[k].item->barcode, item[k].item->name, item[k].item->price, item[k].qty, item[k].tot); 
}