2017-03-05 77 views
-1

該程序需要計算物品的總成本,並向用戶顯示作爲帳單的產品數量,產品數量和價格。請記住,我只是C編程的初學者,因此使用基本功能。程序運行然後表示它停止工作

#include<stdio.h> 
#include<string.h> 
typedef struct 
{ 
    float price; 
    int quantity; 
    char product; 
}Billing; 
typedef struct 
{ 
    int code; 
    char product; 
    float price; 
}Stocks; 
main() 
{ 
    Stocks A; 
    Billing B; 
    FILE*s; 
    FILE*c; 
    printf(" Welcome to I.A.M Cash & Carry\n#8 New Trincity Industrial Estate\nTrincity\n"); 
    printf("=================================\n"); 
    void transfer(Billing,Stocks,FILE*,FILE*); 
    transfer(B,A,s,c); 
} 
void transfer(Billing B,Stocks A,FILE*s,FILE*c) 
{ 
    int count; 
    s=fopen("C:\\stock.txt","r"); 
    int counter=0; 
    while(fgetc(s)!=EOF) 
    { 
     counter++; 
    } 
    fclose(s); 
    count=counter; 
    Stocks D[count]; 
    int x; 
    s=fopen("C:\\stock.txt","r"); 
    for(x=0;x<count;x++) 
    { 
     fscanf(s,"%d",&D[x].code); 
     fscanf(s,"%s",D[x].product); 
     fscanf(s,"%f",&D[x].price); 
    } 
    fclose(s); 
    void menu(Billing,Stocks[],FILE*,FILE*,int); 
    menu(B,D,s,c,count); 
} 
void menu(Billing B,Stocks D[],FILE*s,FILE*c,int count) 
{ 
    printf("1)Enter items for billing\n"); 
    printf("2)Quit\n"); 
    void choice(Billing,Stocks[],FILE*,FILE*,int); 
    choice(B,D,s,c,count); 
} 
void choice(Billing B,Stocks D[],FILE*s,FILE*c,int count) 
{ 
    int x; 
    printf("Enter choice:\n"); 
    scanf("%d",&x); 
    if((x<1) || (x>2)) 
    { 
     printf("Choice invalid\nPlease Re-enter\n"); 
     menu(B,D,s,c,count); 
    } 
    if(x==1) 
    { 
     void submenu(Billing,Stocks[],FILE*,FILE*,int); 
     submenu(B,D,s,c,count); 
    } 
    if(x==2) 
    { 
     printf("Thank you, Have a great day\n"); 
    } 
} 
void submenu(Billing B,Stocks D[],FILE*s,FILE*c,int count) 
{ 
    int x,z,quan,prod; 
    float sum=0; 
    float prices=D[x].price*quan; 
    c=fopen("C:\\bill.txt","w"); 
    do{ 
    printf("Select choice:\n"); 
    printf("1)Enter items\n"); 
    printf("2)Quit"); 
    scanf("%d",&x); 
    if(x==1) 
    { 
     printf("Enter product code:"); 
     scanf("%d",&prod); 
     for(z=0;z<count;z++) 
     { 
      if(prod==D[x].code) 
      { 
      printf("Enter quantity:"); 
      scanf("%d",&quan); 
      fprintf(c,"%d ",quan); 
      fprintf(c,"%s ",prod); 
      fprintf(c,"%5.2f\n",D[x].price*quan); 
      sum= sum + prices; 
      } 
     } 
    } 
    if(x==2) 
    { 
     printf("Thank you, Have a great day\n"); 
    } 
}while(x!=2); 
fclose(c); 
void countbill(Billing,FILE*,float); 
countbill(B,c,sum); 
} 
void countbill(Billing B,FILE*c,float sum) 
{ 
    int y; 
    c=fopen("C:\\bill.txt","r"); 
    int x=0; 
    while(fgetc(c)!=EOF) 
    { 
     x++; 
    } 
    fclose(c); 
    y=x; 
    Billing V[y]; 
    void displaybill(Billing[],FILE*,float,int); 
    displaybill(V,c,sum,y); 
} 
void displaybill(Billing V[],FILE*c,float sum,int y) 
{ 
    int x; 
    c=fopen("C:\\bill.txt","r"); 
    for(x=0;x<y;x++) 
    { 
     fscanf(c,"%d",&V[x].quantity); 
     fscanf(c,"%s",V[x].product); 
     fscanf(c,"%f",&V[x].price); 
     printf("%d %s %f\n",V[x].quantity,V[x].product,V[x].price); 
    } 
    printf("Total=%f",sum); 
} 

調試器輸出

Debugger output

+0

你有調試嗎?你得到什麼錯誤? – Carcigenicate

+0

學習使用調試器。它將在未來得到回報。 –

+0

@Carcigenicate如果我點擊調試,我的編譯器崩潰。 – iGunta

回答

0

對不起IM使這個作爲一個答案,我只是想你真正閱讀。 這段代碼有一大堆問題,生病只是澄清大部分:

1)你是不是宣佈你的任何功能

2)你叫你的大部分功能的方式是錯誤的

3 )你正在使用大量的變量沒有初始化它們,就像x

4)你不能在數組聲明中放置一個非常量變量,它必須是一個常數,或者使用動態分配。 (例如Stocks D[count]

這只是我從一般的外觀中看到的。

它看起來像你有一些功課要做,然後再繼續這段代碼。

希望它以某種方式幫助。