我想用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); '''char a;'是災難的祕訣,你告訴'scanf()'有足夠的空間在一個'char'中至少有兩個字符(一個字母和一個空字節來終止字符串) - 並且沒有,你應該可以使用'scanf('%c',&a);''''''''之前的空格是至關重要的 - 它會跳過流浪的換行符和其他空格。 –
當你選擇條形碼0時,你的代碼並不是很高興。 –