2013-09-23 39 views
-1

我編寫了一個程序來跟蹤雜貨店的庫存。但在我的代碼中,我想打印一個值。價值是(單位數量)*(單位價格)。但不知何故,我得到了我的程序中的垃圾值。所以你能幫我嗎?雜貨店的庫存

#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(); 
} 
+0

@PaulGriffiths:我也這麼認爲,但如果你讀碼再次,你會發現他沒有使用'i'來爲數組建立索引,而是'j',它的適當範圍從'0'到'y-1'。 – Dolda2000

+0

我用J,所以我可以打印值[0],值[1] ......值[4]。但問題是當我乘以(單位數量)*(單位價格)時,我得到了無意義的數字。我不明白。請你幫幫我。謝謝。 – Heman

+0

@ Dolda2000:你是對的,我刪除了評論。 –

回答

5

的問題似乎是,你會錯誤地包含在你for循環的一個分號結尾:

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

它被稱爲「一個真正的支撐風格」*理由* ;-) –

+0

是不是這樣。 :) – Dolda2000

+0

Thnak你這麼多Dolda2000。你真好! – Heman