c - 杂货店的库存

标签 c

我编写了一个程序来跟踪杂货店的库存。但在我的代码中我想打印一个值。值(value)为(单位数量)*(单价)。但不知怎的,我在我的程序中得到了垃圾值。那么你能帮我吗?

#include<stdio.h>
#include<conio.h>
#define MAX 5
int printinventory(int , int unit[] , float price[]);
int main()
{
    int item[MAX],unit[MAX],x,i;
    float price[MAX];
    printf("Please enter how many category items (up to 5) : ");
    scanf("%d",&x);
    for(i=0;i<x;i++)
    {
    printf("\nPlease enter Number of Units #%d : ",i+1);
    scanf(" %d",&unit[i]);
    printf("\nPlease enter the Unit Price #%d : ",i+1);
    scanf(" %f",&price[i]);
    }
    printinventory(x , unit , price);
    getch();
}

int printinventory (int  y, int unit[] , float price[])
{
    int i,j=0;
    float value[MAX];
    for(i=0;i<y;i++);
    {
        value[i] = (unit[i] * price[i]);
    }
    system("cls");
    printf("Item     Number of Units   Unit Price    Value ");
    printf("\n\n------------------------------------------------");
    for(i=1;i<=y;i++)
    {
        printf("\n%d",i);
        printf("\t  %d",unit[j]);
        printf("\t\t    $%.2f",price[j]);
        printf("\t$%.2f",value[j]);
        j++;
    }
    printf("\n\n------------------------------------------------");
    printf("\n\t\t\t\tTotal   $   ");
    getch();
}

最佳答案

问题似乎是您错误地在 for 循环之一的末尾添加了分号:

    for(i=0;i<y;i++);

关于c - 杂货店的库存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950286/

相关文章:

c - 没有这样的文件或目录,但文件存在

c - 在 C 中打印 trie 中的单词

c - 为什么我的程序打印出错误的十进制数

c - SHA1 以流方式对长度前缀的消息进行校验和

c - 如何获得复杂的 C 预处理器行为以简化动态程序汇编

c char指针内存和全局变量

c - 为什么结果不是预期的?

c - 程序返回字符而不是字符串

c - Perl(或C printf)后退一个选项卡,可能吗?

c - 如何在同时使用另一个循环的同时使用循环递增变量?