add/remove a field to structure

This is a discussion on add/remove a field to structure within the C Programming forums, part of the General Programming Boards category; This is what i have so far. I need to write a function to add and remove a field from ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    add/remove a field to structure

    This is what i have so far. I need to write a function to add and remove a field from a file. I called the add function in main but do not know what to do next.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    typedef struct moto
    {
    	char make[10];
    	char model[10];
    	char trim[10];
    	int year;
    	int price;
    	struct moto* next;
    } MOTO;
    
    char menu(void);
    MOTO* getdata (MOTO* head);
    MOTO* position(MOTO* head, MOTO* adding);
    void print(MOTO* head);
    void add(MOTO* arrival);
    void tofile(MOTO* head);
    
    int main (void)
    {
    	MOTO* head = NULL;
            MOTO* arrival;
    	char userop;
    	int num;
    
    	head = getdata(head);
    	print(head);
    	
    	while ((userop=menu()) != 'Q')
    	{
    		switch(userop)
    		{
    			case 'P': print(head);
    				break;
    			case 'A':
    			{
    				add(&arrival);
    				                        // <--- what goes here?
    			}
    				break;
    			case 'R':
    				break;
    			case 'M':
    				break;
    			case 'O': tofile(head);
    				break;
    			default:  printf("\n Invalid.  Enter choice again"); 
    				break;
    		}
    	}
    
    return 0;
    }
    
    void add(MOTO* arrival)
    {
    
    	printf("Enter the year of the motocycle: ");
    	scanf("%d",&arrival->year);
    	printf("Enter motocycle make: ");
    	scanf("%s",arrival->make);
    	printf("Enter motocycle model: ");
    	scanf("%s",arrival->model);
    	printf("Enter motocycle trim: ");
    	scanf("%s",arrival->trim);
    	printf("Enter motocycle price: ");
    	scanf("%d",&arrival->price);
    	
    	return;
    }
    
    MOTO* getdata (MOTO* head)
    {
    
    	FILE* data;
    	MOTO* ptr;
    	char temp [255];
    
    	if ((data=fopen ("moto.txt", "r")) == NULL)
    	{
    		printf("Error opening file.");
    		exit(100);
    	}
    	while (fgets (temp, sizeof(temp), data) != NULL)
    	{
    		ptr = (MOTO*) malloc (sizeof (MOTO));
    		sscanf (temp, "%d%s%s%s%d",&ptr->year, ptr->make, ptr->model, ptr->trim, &ptr->price);
    
    		ptr->next = NULL;
    		head = position(head, ptr);
    	}
    	fclose (data);
    
    	return head;
    }
    
    MOTO* position(MOTO* head, MOTO* adding)
    {
    	MOTO* current;
    	MOTO* previous;
    
    	if(!head)
    		head = adding;
    	else
    	{
    		current = head;
    		previous = NULL;
    		while (current && adding->year < current->year)
    		{
    			previous = current;
    			current = current->next;
    		}
    		if (previous)
    		{
    			adding->next = current;
    			previous->next = adding;
    		}
    		else
    		{
    			adding->next = head;
    			head = adding;
    		}
    	}
    
    return head; 
    }
    
    void print(MOTO* head)
    {
    	MOTO* walker;
    	walker = head;
    
    	printf("\n");
    	while(walker)
    	{	
    		printf("%d %s %s %s $%d\n", walker->year, walker->make, walker->model, walker->trim, walker->price);
    		walker=walker->next;
    	}
    
    	return;
    }
    
    void tofile(MOTO* head)
    {
    	FILE* fout;
    	MOTO* walker;
    	walker = head;
    
    	fout = fopen("motoout.txt","w");
    
    	while(walker)
    	{
    		fprintf(fout,"%d %s %s %s %d \n",walker->year, walker->make, walker->model, walker->trim, walker->price);
    		walker = walker->next;
    	}
    
    	fclose(fout);
    	printf("Data printed to file. \n");
    	return;
    }
    
    char menu(void)
    {  
    	char choice;
    	
    	printf("\n**********************************************");
    	printf("\n1. Enter 'P' to print the list of motorcycles");
    	printf("\n2. Enter 'A' to add a motorcycle");
    	printf("\n3. Enter 'R' to remove a motorcycle");
    	printf("\n4. Enter 'M' to modify a field.");
    	printf("\n5. Enter 'O' to output to file.");
    	printf("\n6. Enter 'Q' to quit.");
    	printf("\n**********************************************");
    	printf("\nPlease enter a choice: ");
    	scanf(" %c",&choice);
    
    	return toupper(choice);
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    This is what i have so far. I need to write a function to add and remove a field from a file. I called the add function in main but do not know what to do next
    A somewhat vague request. What goes there? I don't know. What do you want it to do?

    You've asked for input from the user and placed it inside the struct object. I imagine you might want to add it to the list so you don't lose it.

    Incidentally, I'm quite tired, so I could be very far off the mark, but I think you're going to have severe pointer issues when you try to compile and run this.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Ok nevermind. I got it.
    Link lists makes my head hurts. Thnks anyways.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please critique and suggest improvements for parser
    By Queue in forum C Programming
    Replies: 14
    Last Post: 09-28-2006, 08:28 PM
  2. Use variable to specify structure field
    By Rick87 in forum C Programming
    Replies: 10
    Last Post: 03-19-2006, 12:17 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21