Thread: programming in C

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    programming in C

    One of the requirments was to use a menu to allow the user to select the following:
    1:read a members detail, specified by number
    2:display inactive members on screen.

    please have a go
    Code:
    /* -----------------------------------------------------------------------------------
    * Name:	sample.c
    * Function: 
    * Developed By:	
    * Date: 
    * History: Modified By:	Reason: Date:
    --------------------------------------------------------------------------------------*/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    // ------------------- New types declared - Global types.-------------------
    struct addr
    {
     	char first_name[30];
     	char surname[40];
     	char address1[40];
     	char county[50];
     	char country[30];
    	int  sex;			// 1 for female, 0 for male
    	int  active_member;
    	int  member_num;
    };
    
    // ----------------------Global Variables-----------------------
    FILE *fp;
    
    // ----------------- Function prototypes ----------------------
    
    int main(void);
    void details(struct addr *, int);	// Using pass by reference for structure.
    void store_details(struct addr);	//Write to disk.
    int  file_size(void);				// Returns number of members in file.
    
    // ------------------------------------------------------------
    
    int main(void)
    {
    	struct addr member;			// Declare variable to use.
    	int i;
    	char ans;
    
    	// Open file for reading and writing - Append or create file 
    	if ((fp = fopen("store.bin", "ab+")) == NULL)
    	{
    		printf("Cannot open file \"store.bin\" for writing....\n");
    		exit(1);
    	}
    
    	// Enter details for all members. - forever loop
    	for (i = file_size(); ; i++)	
    	{
    		details(&member, i);	// Function call
    		store_details(member);
    
    		printf("Enter more members - Y/N : ");
    		ans = getche();
    		printf("\n\n\n");
    		if ((ans == 'N') || (ans == 'n'))
    			break;
    	}
    	
    	printf("\n\n");
    
    	return 0;
    
    }
    
    /* -----------------------------------------------------------------
    	Function to get and store the details of each member.
    	Called:	details(struct addr *);
    	IN:		struct addr *: Address  of structure addr to store details in.
    			int : member number;
    	OUT:	Filled structure.
     ---------------------------------------------------------------- */
    
    void details(struct addr *member, int i)
    {
    	printf("Enter first name for member &#37;d: ", i);
    	scanf("%s", member->first_name);
    
    	printf("Enter surname for member %d: ", i);
    	scanf("%s", member->surname);
    
    	printf("Enter the first line of address for member %d: ", i);
    	scanf("%s", member->address1);
    		
    	printf("Enter county for member %d: ", i);
    	scanf("%s", member->county);
    			
    	printf("Enter country for member %d: ", i);
    	scanf("%s", member->country);
    			
    	printf("Is member %d active (1=Y/0=N): ", i);
    	scanf("%d", &member->active_member);
    		
    	printf("Is member %d male or female (1=Y/0=N): ", i);
    	scanf("%d", &member->sex);
    
    	member->member_num = i;
    
    	printf("\n\n");
    }
    
    /* -----------------------------------------------------------------
    	Function to store the details of each employee to disk.
    	Called:	store_details(struct addr);
    	IN:		struct addr: Structure addr to store to disk.
    	OUT:	Structure wrote to File.
     ---------------------------------------------------------------- */
    void store_details(struct addr member)
    {
    	// Seek to end of file before writing.
    	fseek(fp, 0, SEEK_END);
    
    	// Write structure to file - Appending to end of file if file exists.
    	fwrite(&member, sizeof(struct addr), 1, fp);
    }
    
    /* -----------------------------------------------------------------
    	Function to return numbers of member structures in file.
    	Called:	file_size;
    	IN:		void.
    	OUT:	void.
    	returns : numbers of members in file.
     ---------------------------------------------------------------- */
    int file_size(void)
    {
    	// Seek to end of file before writing.
    	fseek(fp, 0, SEEK_END);
    
    	// number of bytes in file divided by size of structure = number of structures in file.
    	return(ftell(fp) / sizeof(struct addr)); 
    
    }
    o.
    Last edited by Salem; 04-12-2008 at 01:55 PM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use code tags, use proper indentation, show a little respect to people you ask questions

    PS. Read this http://cboard.cprogramming.com/showthread.php?t=25765
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed