current position:Home>Help me look at the code (I'm just a freshman, so I only know C). Why the bill The data in txt will always become 0 (after each command 1 is executed and more than one order is entered, only the last order in txt is saved, and all the others become 0) (o

Help me look at the code (I'm just a freshman, so I only know C). Why the bill The data in txt will always become 0 (after each command 1 is executed and more than one order is entered, only the last order in txt is saved, and all the others become 0) (o

2022-02-02 19:20:17 CSDN Q & A

#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 200

typedef struct date
{
int year;
int month;
int day;
}DATE;

typedef struct bill
{
char billName[100];
double money;
DATE time;
long long int billID;
char billSort[20];
char note[100];
}BILL;

void web(void);
void PrintMessage(BILL stu[],int i);
void Exchange(BILL temp[],BILL stu[],int i,int j);
void WritetoFile(BILL stu[],int z);
int ReadfromFile(BILL stu[]);

int main()
{
BILL temp[200];
int i=0;int z=0;int n,j;
int count=0;

DATE timech;int x=0;double Sum[13]={
   
   
   
   0,0,0,0,0,0,0,0,0,0,0,0,0}; double S=0;float P[100]={};double sum1=0;double sum2=0;double sum3=0;double sum=0;float p1,p2,p3; long int max,min;BILL stu [N];

WEB: web();
int SLN;
scanf("%d",&SLN);

switch(SLN){    case 1:z=0;printf(" Please start entering the bill .\n"); char a;do {    printf("\n  Bill summary :");     getchar();    gets(stu[i].billName);    printf("  amount of money :");    scanf("%lf",&stu[i].money);    printf("  date :");    scanf("%d",&stu[i].time.year);    scanf("%d",&stu[i].time.month);    scanf("%d",&stu[i].time.day);    printf("  The order number :");    scanf("%lld",&stu[i].billID);    printf(" *** Please from “ consumption ”“ Loan repayment ”“ Human relations ” Choose among the three .***\n");    printf("  Bill classification :");    scanf("%s",stu[i].billSort);    printf("  remarks :");    scanf("%s",stu[i].note);    z++;    printf("\n Enter next item (Y/N)\n");    scanf(" %c",&a);    while(a!='Y'&&a!='N')// Prevent illegal import      {    printf(" illegal input , Please enter the correct instruction \n");    getchar();    scanf("%c",&a);    }

}while(a=='Y');// Refuse to enter the next item
WritetoFile(stu,z);
goto WEB;

    case 2: n=ReadfromFile(stu); count=n;// Chronological array  for(j=0;j<count-1;j++)// Annual ranking   {     for(i=j+1;i<count;i++)     {     if(stu[j].time.year>stu[i].time.year)     {         Exchange(temp,stu,i,j);     }    } } for(j=0;j<count-1;j++)// Monthly sorting   {     for(i=j+1;i<count;i++)     {     if(stu[j].time.month>stu[i].time.month)      {         Exchange(temp,stu,i,j);     }    } } for(j=0;j<count-1;j++)// Daily sorting   {     for(i=j+1;i<count;i++)     {     if(stu[j].time.year>stu[i].time.year)      {        Exchange(temp,stu,i,j);     }    } } for(i=0;i<count;i++) {     printf("\n----------------------------------- \n");    PrintMessage(stu,i);    printf("\n");      } goto WEB;            case 3:    n=ReadfromFile(stu);    // Query material information by date     printf(" Please enter the date you want to find :\n");    scanf("%d%d%d",&timech.year,&timech.month,&timech.day);     for(i=0;i<n;i++){     if(timech.year==stu[i].time.year&&timech.month==stu[i].time.month&&timech.day==stu[i].time.day)     {    printf("\n----------------------------------- \n");    PrintMessage(stu,i);    printf("\n");     x++;    }  }    if(x==0)    printf(" There is no bill corresponding to this date ");    x=0;// Empty x Value to find again        goto WEB;                       case 4:    n=ReadfromFile(stu);                for(i=0;i<n;i++)   {           for(j=1;j<=12;j++)           {         if(stu[i].time.month==j)        {             Sum[j]=Sum[j]+stu[i].money;        }         }          S=S+stu[i].money;   }         for(i=1;i<=12;i++)        {             P[i]=Sum[i]*1.0/S;             printf("%d The proportion of monthly consumption is :%0.1f%%\n",i,P[i]*100);        }    goto WEB;        case 5:        n=ReadfromFile(stu);        for(i=0;i<n;i++)   {         if(strcmp(stu[i].billSort," consumption \0")==0)         {             sum1=sum1+stu[i].money;         }        if(strcmp(stu[i].billSort," Loan repayment \0")==0)         {             sum2=sum2+stu[i].money;         }         if(strcmp(stu[i].billSort," Human relations \0")==0)        {             sum3=sum3+stu[i].money;         }                  sum=sum+stu[i].money;   }        p1=sum1*1.0/sum;        p2=sum2*1.0/sum;        p3=sum3*1.0/sum;        printf(" The proportion of consumption is :%0.1f%%\n",p1*100);        printf(" The proportion of loan and repayment is :%0.1f%%\n",p2*100);        printf(" The proportion of human relations is :%0.1f%%\n",p3*100);      goto WEB;        case 6: n=ReadfromFile(stu);  max=stu[0].money; min=stu[0].money; for(i=1;i<n;i++) {     if(stu[i].money>max)     max=stu[i].money;     if(stu[i].money<min)     min=stu[i].money; } for(i=0;i<n;i++) {     if(stu[i].money==max)     {     printf("\n------------------------------\n");     printf(" The highest record is :\n");    PrintMessage(stu,i);    }    if(stu[i].money==min)    {     printf("\n------------------------------\n");     printf(" The lowest record is :\n");    PrintMessage(stu,i);        }} goto WEB;        default :            printf(" Your input is wrong !\n"); goto WEB;        case 0:        exit(0);        }return 0;     

}

void web(void)
{
printf("\n Alipay billing information management system \n");
printf(" \n 1. Bill information entry \n");
printf(" 2. Sort and output bill information by date \n");
printf(" 3. Query bill information by date \n");
printf(" 4. Statistics of consumption amount by month \n");
printf(" 5. Statistical analysis by bill category \n");
printf(" 6. Query the bills with the least amount and the most amount \n");
printf(" 0. Exit the system \n");
printf(" \n Tips : Please enter the numerical sequence number and select the corresponding operation ");
}

void PrintMessage(BILL stu[],int i)
{
printf(" Bill summary :%s\n",stu[i].billName);
printf(" amount of money :%.1f\n",stu[i].money);
printf(" date :%3d-%02d-%02d\n",stu[i].time.year,stu[i].time.month,stu[i].time.day);
printf(" The order number :%I64d\n",stu[i].billID);
printf(" Bill classification :%s\n",stu[i].billSort);
printf(" remarks :%s\n",stu[i].note);
}

void Exchange(BILL temp[],BILL stu[],int i,int j)
{
temp[i]=stu[i];
stu[i]=stu[j];
stu[j]=temp[i];

}

void WritetoFile(BILL stu[],int z)
{
FILE*fp;
if((fp=fopen("bill.txt","a"))==NULL)
{
printf(" The file was not found !\n");
exit(0);
}
fwrite(stu,sizeof(BILL),z,fp);
fclose(fp);
}

int ReadfromFile(BILL stu[])
{
FILE*fp;
int i;
if((fp=fopen("bill.txt","r"))==NULL)
{
printf(" The file was not found !\n");
exit(0);
}

for(i=0;!feof(fp);i++){    fread(&stu[i],sizeof(BILL),1,fp); } fclose(fp); return i-1; 

}




Refer to the answer 1:

you are here do while Use... In circulation stu[i] Record everyone's information , But this i Always during the cycle 0 ah , What you add is z No i, So it's equal to always modifying the information of the same person . hold z++ Change to i++, And put it after all the data is input
Or put all i Change to z, Because what you write to the file is z




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021920007721.html

Random recommended