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