Hey, Im new to this site and to say it frankly, Im not so good in this programming stuff...
But lately, I have been working on a C program about daily expenses...
at first it was a bit easy but then it gets complicated and I dont know where to ask for help anymore... I have been trying my best to figure out how to write the perfect code for it and ummm sadly, I ran into so many errors...
its about sorting the data according to dates and summing up all the expenses...
could someone please help me to check my codes and help me???
Here are the codes that has been written by me...
Code:
/*Expense Book */
#include <stdio.h>
#include<string.h>
#define FLUSH while (c=getchar()!='\n')
int c;
char AddEntry();
char Delete();
char Display();
char AddAll();
typedef struct
{
char Discription[50];
char Date[20];
double Expenses[50];
} DATA;
DATA data = {0};
int main (void)
{
int i =1;
int option ;
while(i>0)
{
i++;
printf(" \t*******************************************");
printf("\n\t* MENU *");
printf("\n\t* *");
printf("\n\t* 1. ADD ENTRY *");
printf("\n\t* 2. DELETE ENTRY *");
printf("\n\t* 3. DISPLAY ENTRY *");
printf("\n\t* 4. ADD ALL EXPENSES *");
printf("\n\t* 5. EXIT *");
printf("\n\t* *");
printf("\n\t*******************************************");
printf("\n\nPlease type your choice and key return:");
scanf("%d", &option);
printf("You selected option %d\n", option);
if (option == 1)
AddEntry();
else if(option == 2)
Delete();
else if(option == 3)
Display();
else if(option == 4)
AddAll();
else if(option == 5)
{
return 0;
}
else
printf("You may have pressed the wrong key \n");
}
return 0;
}
char AddEntry()
{
FILE *file;
file = fopen("text.txt","a");
printf("Please enter the Discription:");
FLUSH;
gets(data.Discription);
printf("Please enter Date: ");
gets(data.Date);
printf("Please enter Expenses in RM: ");
gets(data.Expenses);
printf("\n");
fwrite (&data, sizeof(DATA), 1, file);
fclose (file);
printf("%s's Expenses has been added.\n\n", data.Discription);
return 0;
}
char Delete()
{
int option;
FILE *file;
printf("1. Delete all of the data.\n");
printf("2. Delete one of the data.\n");
printf("Please enter your choice : ");
scanf ("%d", &option);
printf("\n");
if(option == 1)
{
file = fopen("text.txt","w");
fclose(file);
printf("\n");
}
else if (option == 2)
{
FILE *temp;
char iDiscription[50]={0};
FILE *file;
file = fopen("text.txt", "r");
temp = fopen("temp.txt", "w");
printf("Please enter the discription\nthat you want to delete : ");
FLUSH;
gets (iDiscription);
printf("\n");
while(fread(&data, sizeof(DATA), 1, file))
{
if(strcmp(iDiscription, data.Discription)!=0)
{
fwrite(&data, sizeof(DATA), 1, temp);
}
if(strcmp(iDiscription, data.Discription)==0)
printf("The %s's expenses has been deleted.", data.Discription);
}
fclose(file);
fclose(temp);
file=fopen("text.txt", "w");
temp=fopen("temp.txt", "r");
while(fread(&data, sizeof(DATA), 1, temp))
{
fwrite(&data, sizeof(DATA), 1, file);
}
fclose(file);
fclose(temp);
printf("\n\n");
return 0;
}
else
printf ("\n\a\aERROR, please enter numbers from 1 or 2\a\a\n\n");
return 0;
}
char Display()
{
FILE *file;
file = fopen("text.txt", "r");
while(fread(&data, sizeof(DATA), 1, file))
{
printf("Discription : ");
printf("%s\n", data.Discription);
printf("Date : ");
printf("%s\n", data.Date);
printf("Expenses : ");
printf("RM%s\n", data.Expenses);
printf("\n");
}
fclose(file);
return 0;
}
char AddAll()
{
int i;
double total=0;
double Expenses[50]={1.6, 2.6, 3};
for(i=0; i<3; i++)
{total += Expenses[i];
printf("%.2lf \n", Expenses[i]);}
printf("total is: %.2lf\n\n", total);
return 0;
}
Thank you...