我編寫了一個程序來跟蹤雜貨店的庫存。但在我的代碼中,我想打印一個值。價值是(單位數量)*(單位價格)。但不知何故,我得到了我的程序中的垃圾值。所以你能幫我嗎?雜貨店的庫存
#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();
}
@PaulGriffiths:我也這麼認爲,但如果你讀碼再次,你會發現他沒有使用'i'來爲數組建立索引,而是'j',它的適當範圍從'0'到'y-1'。 – Dolda2000
我用J,所以我可以打印值[0],值[1] ......值[4]。但問題是當我乘以(單位數量)*(單位價格)時,我得到了無意義的數字。我不明白。請你幫幫我。謝謝。 – Heman
@ Dolda2000:你是對的,我刪除了評論。 –