2016-12-28 3 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);を使用して'scanf()'には、少なくとも2文字(文字列と文字列を終了するためのヌルバイト)のための十分なスペースがあることを単一の 'char'で伝えています。あなたはおそらく'scanf("%c "、&a);' '%'の前のスペースは重要です。改行やその他の空白をスキップします。 –

+0

バーコード0を選択すると、コードが恐ろしくうまくいきません。 –

答えて

0

C/C++のインデックスは0から開始していない1.

ibarcode [10]が0から9

+0

まだ動作していません – 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); 
} 
関連する問題