C 为什么不根据记录显示输出?

C 为什么不根据记录显示输出?,c,algorithm,C,Algorithm,我想用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",

我想用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");
}
#包括
#包括
结构项
{
字符名[10];
国际价格;
国际条码;
};
结构项详细信息[10]={
“第1项”、第10项、第1项、,
“项目2”、20、2、,
“项目3”、30、3、,
“项目4”、40、4、,
“第1项”、第50项、第5项、,
“项目2”、60、6、,
“项目3”、70、7、,
“项目4”、80、8、,
“项目3”、90、9、,
“项目4”、100、10
};
int main()
{
int-ibarcode[10];
整数数量[10];
int-tot[10];
int j,i,k,grand;
字符a;
printf(“程序Kasir\n”);
对于(j=0;j<10;j++)
{
printf(“ebter条码:”);
scanf(“%d”和&ibarcode[j]);
对于(i=0;i<10;i++)
{
如果(ibarcode[j]==详细信息[i].条形码)
{
printf(“项目:%s\n”,详细信息[i]。名称);
printf(“价格:%d\n”,详细信息[i]。价格);
printf(“输入数量:”);
扫描量(“%d”和数量[j]);
tot[j]=明细[j]。价格*数量[j];
}
如果(ibarcode[j]>10)
{
printf(“条形码无效”\n”);
j--;
打破
}
}
printf(“\N是否再次购买?[Y/N]=”);
scanf(“%s”、&a);
如果(a==“Y”| | a==“Y”)
{
继续;
}否则
{
打破
}
}
grand=0;
系统(“cls”);
printf(“\n name Kasir=Addzifi Moch G\n”);
printf(“Tanggal=2017年1月3日”);
printf(“Jam=14:05 WIB\n\n”);
printf(“+---------------------------------------------------------------------------------------------------------------+\n”);
printf(“|条形码|项目\t\t\t |价格\t\t |数量\t |总计\n”);
printf(“+---------------------------------------------------------------------------------------------------------------+\n”);

对于C/C++中的(k=0;k),索引从0开始,而不是从1开始


ibarcode[10]表示有从0到9的索引。

问题在于打印收据时,您没有将正确的索引用于
详细信息。您正在打印
详细信息[k]中的字段
,但
k
不是客户购买的商品的索引,它只是
for()
循环的当前迭代

您需要保存在第一个循环中搜索
detail
时找到的索引
i
,以获取价格

与其使用大量单独的数组,不如使用另一个包含购买详细信息的结构。它可以使用指针引用
详细信息
数组中的项目

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;
    } 
}
(j=0;j<10;j++)的
{
国际条码;
scanf(“%d”和条形码);
int item_found=0;
对于(i=0;i<10;i++)
{
如果(条形码==详细信息[i]。条形码)
{
整数数量;
printf(“项目:%s\n”,详细信息[i]。名称);
printf(“价格:%d\n”,详细信息[i]。价格);
printf(“输入数量:”);
扫描量(“%d”,数量);
物料[j]。数量=数量;
物料[j].总计=数量*deatail[i].价格;
项目[j]。项目=&详细信息[i];
找到的项目=1;
打破
}
}
如果(!找到项){
{
printf(“条形码无效”\n”);
j--;
打破
} 
}
然后,您可以在打印收据时访问详细信息:

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);
}
for(k=0;k条码,物料[k]。物料->名称,物料[k]。物料->价格,物料[k]。数量,物料[k]。tot);
}

@AddzifiMochGumelar这是对你的代码的一般性评论。顺便说一句,我们不是“兄弟”。他没有将输入用作数组索引,而是将其与
细节[i]进行比较。条形码
@barmar那么如何将输入作为数组?使用
scanf(“%s”,&a)
char a;
是一种灾难。你在告诉
scanf()
单个
字符中至少有两个字符(一个字母和一个空字节来终止字符串)有足够的空间,但实际上没有。您可能应该使用
scanf(“%c”,&a);
其中
%
前面的空格至关重要-它跳过了多余的换行符和其他空白。当您选择条形码0时,您的代码不会非常满意。