Thread: Expenses...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Unhappy Expenses...

    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...

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    There's a lot of gets in there that you'd want to get rid of :
    Why gets is bad.

    Now, what kind of errors are you getting ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Unhappy Expense

    firstly,
    I got this warning when compiling...
    f:\expensesbook.c(80) : warning C4133: 'function' : incompatible types - from 'double [50]' to 'char *'
    secondly,
    I have no idea how to write a code telling my program to sum up all the data...
    thirdly,
    I also have no idea how to sort the data according to date...
    try run the code first and see whether you get what I mean...
    fourth,
    can you explain to me how the code works...
    for I must say I am a complete idiot in understanding the code...
    (but I do understand some of it though)
    I just wrote it according to books and samples...
    finally...
    help... in rewriting the code...
    thank anyway, for telling me that gets is bad... I try to avoid using it next time...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Link list help
    By ofyunus in forum C Programming
    Replies: 7
    Last Post: 01-10-2009, 02:15 PM
  3. Custom PC Business....
    By o0obruceleeo0o in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 07-04-2003, 11:27 AM
  4. formatting output in C++
    By ubershatten in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2002, 10:15 AM