สร้างโปรแกรมบันทึกรายรับรายจ่ายจากCode::Blocksแล้วติดปัญหาเรื่องไฟล์

สวัสดีค่ะ ตอนนี้กำลังทำโปรแกรมบันทึกรายรับรายจ่ายจากCode::Blocks แล้วติดปัญหาที่แก้ไม่ได้สักทีเลยอยากขอความช่วยเหลือจากชาวพันทิปค่ะ TT
โดยในโปรแกรมจะมีการใช้เรื่องการบันทึกค่าดึงค่าจากไฟล์มาเกี่ยวข้อง ซึ่งเมื่อเวลาปิดโปรแกรมไปแล้วเปิดมาอีกครั้ง จะมีการโชว์ค่าสรุปการบันทึกครั้งล่าสุดก่อนปิดโปรแกรมให้
แต่ปัญหาตอนนี้คือ ถ้าบันทึก2เดือนติดกัน โดยไม่ปิดโปรแกรมระหว่างเดือนหรือวันสุดท้ายของเดือน เมื่อเปิดโปรแกรมมาอีกที วันที่ที่โชว์จะขึ้นแบบไม่ถูกต้อง
อันนี้คือรูปที่เปิดโปรแกรมมาอีกครั้งหลังจากบันทึกตั้งแต่วันที่ 1/1/2019 -2/1/2019 แล้วปิดโปรแกรม
อันนี้คือรูปที่เปิดโปรแกรมมาอีกครั้งหลังจากบันทึกตั้งแต่วันที่ 1/1/2019 -3/2/2019 แล้วปิดโปรแกรม
อันนี้คือโค้ดนะคะ

#include<math.h>
#include<string.h>
#include<conio.h>
#include <windows.h>
#include<stdio.h>
int x,y;COORD coord = {0,0};
void gotoxy(short x,short y)
{
COORD pos = {x,y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
main()
{
FILE *Date;
FILE *Month;
FILE *Year;
FILE *remain;
FILE *Conclude;
int year,Income,Total,Food,Trans,Rent,App,Shop,j,m,n,date=0;
char e;
char b[200],c[50];
char a[150];
printf(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n");
printf("::::::::::::::::::::::::::::::::::::: The last recording ::::::::::::::::::::::::::::::::::::::::\n");
printf(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::\n");
char co[500],contin;
int re,mo,a;
Conclude = fopen("conclude.txt","r");
Date = fopen("date.txt","r");
Month = fopen("month.txt","r");
Year = fopen("year.txt","r");
remain = fopen("remain.txt","r");

while (!feof(Date))
{
fscanf(Date,"%d",&da);
printf("\n At %d/",da);
}

while (!feof(Month))
{
fscanf(Month,"%d",&mo);
printf("%d/",mo);
}

while (!feof(Year))
{
fscanf(Year,"%d",&ye);
printf("%d\n\n",ye);
}

while (!feof(Conclude))
{
fgets(co, 60, Conclude);
printf("%s",co);
}

while (!feof(remain))
{
fscanf(remain,"%d",&re);
printf("\n\n Remain = %d",re);
}
printf("\n\n Continue (y/n) : ");
scanf(" %c",&contin);

if(contin!='y'&&contin!='Y')
{
printf("\n\n Continue (y/n) : ");
scanf(" %c",&contin);
}

if(contin=='n')
{ exit(1);
}
fclose(Date);
fclose(Month);
fclose(Year);
fclose(Conclude);
fclose(remain);

if(contin=='y'||contin=='Y')
{
system("cls");
fflush(stdin);
Conclude = fopen("conclude.txt","w");
Date = fopen("date.txt","w");
Month = fopen("month.txt","w");
Year = fopen("year.txt","w");
remain = fopen("remain.txt","w");

int month[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("\n\n***********************************************************************************************************\n");
printf("************************************* Keep a ledger *****************************************\n");
printf("***********************************************************************************************************\n\n");
printf("Enter year (AD) : ");
scanf("%d",&year);
fprintf(Year," %d",year);
printf("Enter number of month (1-12): ");
scanf("%d",&j);
if(da>=month[j])
{
da=0;
j++;
}

if(j!=mo)
{
da=0;
}
fprintf(Month,"%d",j);

gotoxy(46,9);
if (j==1)
{ printf("January , %d",year);}
if (j==2)
{ printf("February , %d",year);}
if (j==3)
{ printf("March , %d",year);}
if (j==4)
{ printf("April , %d",year);}
if (j==5)
{ printf("May , %d",year);}
if (j==6)
{ printf("June , %d",year);}
if (j==7)
{ printf("July , %d",year);}
if (j==8)
{ printf("August , %d",year);}
if (j==9)
{ printf("September , %d",year);}
if (j==10)
{ printf("October , %d",year);}
if (j==11)
{ printf("November , %d",year);}
if (j==12)
{ printf("December , %d",year);}

if(year%400==0&&year0!=0)
{
month[2]= 29;
}

printf("\n---------------------------------------------------------------------------------------------------------------");
printf("\n Day\t Income\t Total\t Expenses\t Remain\n");
printf("---------------------------------------------------------------------------------------------------------------");

int i=0,Remain=0,days,x,y,date,mon;
x=9;
y=13;
int sumfood,sumtrans,sumrent,sumapp,sumshop,sumincome;
sumfood=0;
sumtrans=0;
sumrent=0;
sumapp=0;
sumshop=0;
sumincome=0;
date =0;

fscanf(remain,"%d",&re);
fscanf(Date,"%d",&da);

while(j>0&&j<=12)
{
if(da>=month) da=0;
for(days=da+1; days<=month[j]; days++)
{
gotoxy(x,y);
printf("%d",days);
gotoxy(x+12,y);
printf("Receive : ");
scanf("%d",&Income);
Total = i+Income+Remain+re;

gotoxy(x+35,y);
printf("%d",Total);
re=0;
gotoxy(x+52,y);
printf("Food :\n");
gotoxy(x+73,y);
scanf("%d",&Food);
gotoxy(x+52,y+1);
printf("Transport :\n");
gotoxy(x+73,y+1);
scanf("%d",&Trans);
gotoxy(x+52,y+2);
printf("Utilities/Rent :\n");
gotoxy(x+73,y+2);
scanf("%d",&Rent);
gotoxy(x+52,y+3);
printf("Appliance :\n");
gotoxy(x+73,y+3);
scanf("%d",&App);
gotoxy(x+52,y+4);
printf("Traveling/Shopping :\n");
gotoxy(x+73,y+4);
scanf("%d",&Shop);
Remain = Total-Food-Trans-Rent-App-Shop;
sumfood = Food+sumfood;
sumapp = App+sumapp;
sumrent = Rent+sumrent;
sumshop = Shop+sumshop;
sumtrans = Trans+sumtrans;
sumincome = Income+sumincome;

gotoxy(x+89,y);
printf("%d",Remain);
printf("\n");
y=y+7;

gotoxy(x+89,y-5);
printf("\n\n\n\t\t\t\t\t\t\t\t\t\t\t\tnext ? (y/n) : ");
scanf(" %c",&e);
if(e=='n'||e=='N')
{
fprintf(Date,"%d",days%32);
gotoxy(40,y);
printf("----- CONCLUDE EXPENSES ----- ");
fprintf(Conclude,"\n ----- CONCLUDE EXPENSES ----- ");
printf("\n\n Income = %d",sumincome);
fprintf(Conclude,"\n\n Income = %d",sumincome);
printf("\n Food = %d ",sumfood);
fprintf(Conclude,"\n Food = %d ",sumfood);
printf("\n Transport = %d",sumtrans);
fprintf(Conclude,"\n Transport = %d",sumtrans);
printf("\n Utilities/Rent = %d",sumrent);
fprintf(Conclude,"\n Utilities/Rent = %d",sumrent);
printf("\n Appliance = %d",sumapp);
fprintf(Conclude,"\n Appliance = %d",sumapp);
printf("\n Traveling/Shopping = %d",sumshop);
fprintf(Conclude,"\n Traveling/Shopping = %d",sumshop);
printf("\n\n Remain = %d\n\n",Remain);
fprintf(remain," %d",Remain);

exit(1);
}
}

fprintf(Date,"%d",date);

gotoxy(40,y);
printf("----- CONCLUDE EXPENSES OF MONTH ----- ");
printf("\n\n Income = %d",sumincome);
printf("\n Food = %d ",sumfood);
printf("\n Transport = %d",sumtrans);
printf("\n Utilities/Rent = %d",sumrent);
printf("\n Appliance = %d",sumapp);
printf("\n Traveling/Shopping = %d",sumshop);
printf("\n Remain = %d",Remain);
sumfood=0;
sumtrans=0;
sumrent=0;
sumapp=0;
sumshop=0;
sumincome=0;
j++;
y=y+15;

if(da>=month[j])
{
mo=0;
}
fprintf(Month,"%d",j);

gotoxy(x+38,y-5);
if (j==1)
{ printf("January , %d",year); }
if (j==2)
{ printf("February , %d",year); }
if (j==3)
{ printf("March , %d",year); }
if (j==4)
{ printf("April , %d",year);}
if (j==5)
{ printf("May , %d",year); }
if (j==6)
{ printf("June , %d",year);}
if (j==7)
{ printf("July , %d",year);}
if (j==8)
{ printf("August , %d",year);}
if (j==9)
{ printf("September , %d",year); }
if (j==10)
{ printf("October , %d",year);}
if (j==11)
{ printf("November , %d",year);}
if (j==12)
{ printf("December , %d",year);}
if (j!=12)
{
printf("\n---------------------------------------------------------------------------------------------------------------");
printf("\n Day\t Income\t Total\t Expenses\t Remain\n");
printf("---------------------------------------------------------------------------------------------------------------");
}
fclose(Date);
fclose(Month);
fclose(Year);
fclose(Conclude);
fclose(remain);
da=0;
}
}
}
แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่