Thread: Help??How to solve this error??

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    Help??How to solve this error??

    I am trying to create a link list that will allow user to add car information and it will display the memory of the previous and next node in the list.This means that there will previous and next pointers.Whenever I executed the below I get error that says,request for member... in something not a structure or union.
    Can somebody please help??I don't know how to solve this error??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct cardata{
    	
    	char Carname;
    	char CarModel;
    	char Caryear;
    	
    }CarData;
    
    
    struct node{
    	
        CarData *data;
    	struct node *next;
    	struct node *prev;
    }*start=NULL;
    
    
    
    
    main()
    {
    	int ch;
    	do 
    	{
    		printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
    		printf("\nEnter your choice: ");
    		scanf("%d", &ch);
    		
    		switch(ch)
    		{
    			
    			case 1:
    				insert_first();
    				break;
    			case 2:
    				display();
    				break;
    			case 3:
    				exit(0);
    			default:
    				printf("\n\nInvalid choice. Please try again. \n");
    				
    		}
    		
    	} while(1);
    }
    void insert_first(){
    	struct node *ptr;
    	char carname;
    	char carmodel;
    	char caryear;
    	
    	printf("\n\nEnter the car name: ");
    	scanf("%d", &carname);
    	printf("\n\nEnter the car model: ");
    	scanf("%d", &carmodel);
    	printf("\n\nEnter the car year: ");
    	scanf("%d", &caryear);
    	
    	if(start==NULL){
    		
    		start=(struct node *)malloc(sizeof(struct node));
    		start->data.Carname=carname;
    		start->data.CarModel=carmodel;
    		start->data.Caryear=caryear;
    		start->prev=NULL;
    		start->next=NULL;
    		
    	}else{
    		
    		ptr=start;
    		start=(struct node *)malloc(sizeof(struct node));
    		start.data=(struct node *)malloc(sizeof(struct node));
    		start.data->CarName=carname;
    		start.data->CarModel=carmodel;
    		start.data->Caryear=caryear;
    		start->prev=NULL;
    		start->next=ptr;
    		ptr->prev=start;
    		ptr->next=NULL;
    		
    	}
    	
    }
    
    
    void display()
    {
    	struct node *ptr=start;
    	int i=1;
    	
    	if(ptr == NULL){
    		printf("\nLinklist is empty.\n");
    	}else{
    		printf("\nSr. No\t\tAddress\t\Car Name\t\tCar Model\t\tCar Year\t\tPrevious\tNext\n");
    		while(ptr != NULL){
    			printf("\n%d.\t\t%d\t\t%d\t\t%d\t\t%d\t\t\%d\t\tn", i, ptr, ptr->data.Carname,ptr->data.CarModel,ptr->data.Caryear,ptr->prev,ptr->next);
    			ptr = ptr->next;
    			i++;
    		}
    	}
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    In line 58,60,62 you provide pointer to char while scanf expects pointer to int. It will result in memory overruns (scanf will write 4 bytes where only 1 is available)

    In line 67 and further you are using data as if it is struct, but it is declared as pointer. (it is not initialized pointer - so you cannot write anything till you initialize it.)

    I suggest you start writing smaller parts of your program - compile them and test, make sure that they are working correctly before you continue adding more code to your program
    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

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    Can't figure out why program crashes

    Thanks Vart ,I fixed the suggested errors but I can't figure out why my program crashes when I add %s in the printf statement of the display method.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    typedef struct cardata{
    	
    	char carname;
    	char carmodel;
    	char caryear;
    	
    }CarData;
    
    
    struct node{
    	
        struct cardata data;
    	struct node *next;
    	struct node *prev;
    }*start=NULL;
    
    
    main()
    {
    	int ch;
    	do 
    	{
    		printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
    		printf("\nEnter your choice: ");
    		scanf("%d", &ch);
    		
    		switch(ch)
    		{
    			
    			case 1:
    				insert_first();
    				break;
    			case 2:
    				display();
    				break;
    			case 3:
    				exit(0);
    			default:
    				printf("\n\nInvalid choice. Please try again. \n");
    				
    		}
    		
    	} while(1);
    }
    void insert_first(){
    	struct node *ptr;
    	char carname;
    	char carmodel;
    	char caryear;
    	
    	printf("\n\nEnter the car name: ");
    	scanf("%s", &carname);
    	printf("\n\nEnter the car model: ");
    	scanf("%s", &carmodel);
    	printf("\n\nEnter the car year: ");
    	scanf("%s", &caryear);
    	
    	if(start==NULL){
    		
    		start=(struct node *)malloc(sizeof(struct node));
    		start->data.carname=carname;
    		start->data.carmodel=carmodel;
    		start->data.caryear=caryear;
    		start->prev=NULL;
    		start->next=NULL;
    		
    	}else{
    		
    		ptr=start;
    		start=(struct node *)malloc(sizeof(struct node));
    		start->data.carname=carname;
    		start->data.carmodel=carmodel;
    		start->data.caryear=caryear;
    		start->prev=NULL;
    		start->next=ptr;
    		ptr->prev=start;
    		ptr->next=NULL;
    		
    	}
    	
    }
    
    
    void display()
    {
    	struct node *ptr=start;
    	int i=1;
    	
    	if(ptr == NULL){
    		printf("\nLinklist is empty.\n");
    	}else{
    		printf("\nSr. No\tAddress\tCar Name\tCar Model\tCar Year\tPrevious    Next\n");
    		while(ptr != NULL){
    			printf("\n%d.\t%d\t%s\t%d\t%d\n", i, ptr, ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->prev,ptr->next);
    			ptr = ptr->next;
    			i++;
    		}
    	}
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    See my responses here: Can't figure out why program crashes

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    carname, carmodel, and caryear should be arrays, not single chars, such as

    char carname[32];
    char carmodel[32];
    char caryear[5]; // at least 5 since it holds 4 digits plus the '\0' terminator

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    How to check if the object is NULL

    Thanks to anduril462, I can now compile and run my code very well for the Insert_First method of the linkedlist.Now then, if I want to check if the CarData data object is NULL for my insert_specific method,how can I do it for the code below as the while loop requires this condition.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX_WORD_LENGTH 10
    
    
    typedef struct cardata{
        
        char carname[MAX_WORD_LENGTH];
        char carmodel[MAX_WORD_LENGTH];
        char caryear[MAX_WORD_LENGTH];
        
    }CarData;
    
    
    struct node{
        
        CarData data;
        struct node *next;
        struct node *prev;
    }*start=NULL;
    
    
    
    
    void insert_first(){
        struct node *ptr;
        char carname[MAX_WORD_LENGTH];
        char carmodel[MAX_WORD_LENGTH];
        char caryear[MAX_WORD_LENGTH];
        
        printf("\n\nEnter the car name: ");
        scanf("%s", carname);
        printf("\n\nEnter the car model: ");
        scanf("%s", carmodel);
        printf("\n\nEnter the car year: ");
        scanf("%s", caryear);
        
        if(start==NULL){
            
            start=(struct node *)malloc(sizeof(struct node));
            strcpy(start->data.carname,carname);
            strcpy(start->data.carmodel,carmodel);
            strcpy(start->data.caryear,caryear);
            start->prev=NULL;
            start->next=NULL;
            
        }else{
            
            ptr=start;
            start=(struct node *)malloc(sizeof(struct node));
            strcpy(start->data.carname,carname);
            strcpy(start->data.carmodel,carmodel);
            strcpy(start->data.caryear,caryear);
            start->next=ptr;
            
        }
        
    }
    
    
    void insert_specific()
    {
        int n, item;
        struct node *new , *ptr;
        char carname[MAX_WORD_LENGTH];
        char carmodel[MAX_WORD_LENGTH];
        char caryear[MAX_WORD_LENGTH];
        /*If the linked list is empty*/
        if(start == NULL){
            printf("\n\nLinked list is empty. It must have at least one node. \n");
            
        }else{
        
            /*If the linked list is empty*/
            printf("\n\nEnter INFO after which new node is to be inserted: ");
            scanf("%d", &n);
            printf("\n\nEnter ITEM: ");
            scanf("%d", &item);
            
            /*The node pointed by the start will be pointed by the pointer ptr*/
            ptr = start;
            
            /*If pointer is not dangling pointer*/
            while(ptr != NULL){
                
                /*If the  value of  member variable info pointed by ptr is  0 */
                if(ptr->data==NULL)
                {
                    /*Allocate the memory for the variable of type node that will be pointed by new pointer*/
                    new = (struct node *)malloc(sizeof(struct node));
                    
                    /*Item will be stored into the member variable info in new node */
                    strcpy(new->data.carname,carname);
                    strcpy(new->data.carmodel,carmodel);
                    strcpy(new->data.caryear,caryear);
                    
                    /*The address that was pointed by pt->link will also be now pointed by  new */
                    new->next = ptr->next;
                    
                    /*Pointer ptr will now point to address of new node*/
                    ptr->next= new;
                    
                    printf("\n\nItem inserted: %d", item);
                    return;
                }else{
                    ptr= ptr->next;
                }
            }
        }
    }
    
    
    void display()
    {
        struct node *ptr=start;
        int i=1;
        
        if(ptr == NULL){
            printf("\nLinklist is empty.\n");
        }else{
            printf("\nSr. No  Address    Make     Model     Year    Previous      Next\n");
            while(ptr != NULL){
                printf("\n%d.\t%d    %s    %s      %s  %d\t     %d\n", i, ptr, ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->prev,ptr->next);
                ptr = ptr->next;
                i++;
            }
        }
    }
    
    
    int main(void)
    {
        int ch;
        do 
        {
            printf("\n\n\n1. Insert First\n2. Display\n3. Exit\n");
            printf("\nEnter your choice: ");
            scanf("%d", &ch);
            
            switch(ch)
            {
                
                case 1:
                    insert_first();
                    break;
                case 2:
                    insert_specific();
                    break;    
                case 3:
                    display();
                    break;
                case 4:
                    exit(0);
                default:
                    printf("\n\nInvalid choice. Please try again. \n");
                    
            }
            
        } while(1);
        
        return EXIT_SUCCESS;
    }
    Last edited by i++; 08-01-2013 at 11:01 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by i++ View Post
    Thanks to anduril462, I can now compile and run my code very well for the Insert_First method of the linkedlist.Now then, if I want to check if the CarData data object is NULL for my insert_specific method,
    You just do not have such nodes in your list.
    Each node that is inserted - has a filled Data member.

    I have noted in your code in lines 40-58 - you failed to initialize prev in the else case for new node and for the prev node as well.

    I feel you better join both cases in something generic like

    Code:
            ptr=start; /* could be NULL */
            start=(struct node *)malloc(sizeof(struct node));
            strcpy(start->data.carname,carname);
            strcpy(start->data.carmodel,carmodel);
            strcpy(start->data.caryear,caryear);
            start->next=ptr;
            start->prev=NULL; /* was missing */
    
            if(ptr != NULL) /* need to initialize prev - due to line above it is still NULL */
            {
                ptr->prev = start;
            }
    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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And as addition to the previous post line 2 in it I would change to

    Code:
    start=malloc(sizeof(*start));
    I think the reasons you can find in FAQ if desired
    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

  9. #9
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    How do I copy a value of array of strings to char array?

    Sorry to BUMP this thread but after vart's suggestion I realized insert_specific method will not work so I am now just working insert_first and delete_first.Right now I am trying to get user to choose a car name instead of explicitly stating name and I created a struct CriteriaSelector in which array name companyList will store value of companies and when I let user choose car name it will copy a string at a particular index into the char array called carname which will then be copied into carname char array in the CarData object.

    The thing is whenever I compile the below code, I get the error saying expected specifier qualifier list before 'companyList' at line 27 and I don't know what to do?
    Can anybody help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #define MAX_WORD_LENGTH 20
    
    
    typedef struct cardata{
    	
    	char carname[MAX_WORD_LENGTH];
    	char carmodel[MAX_WORD_LENGTH];
    	char caryear[MAX_WORD_LENGTH];
    	char cartype[MAX_WORD_LENGTH];
    	int  quantity;
    	
    }CarData;
    
    
    struct node{
    	
        CarData data;
    	struct node *next;
    	struct node *prev;
    }*start=NULL;
    
    
    
    
    typedef struct criteriaselector{
    	
    	const char *companyList[10];
    	companyList[0] = "Toyota";
    	companyList[1] = "Honda";
    	companyList[2] = "Hyundai";
    	companyList[3] = "Nissan";
    	companyList[4] = "Mitsubishi";
    	companyList[5] = "Volksvagon";
    	companyList[6] = "Acura";
    	companyList[7] = "Ford";
    	companyList[8] = "Dodge"
    	companyList[9] = "GMC";
    		
    }CriteriaSelector;
    
    
    void insert_first(){
    	struct node *ptr;
    	CriteriaSelector criteriaselector;
    	char carname[MAX_WORD_LENGTH];
    	char carmodel[MAX_WORD_LENGTH];
    	char caryear[MAX_WORD_LENGTH];
    	char cartype[MAX_WORD_LENGTH];
    	int  carQuantity;
    	int ch;
    	printf("\nChoose your car");
    	printf("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5. Mitsubishi \n6. Volksvagon \n7. Acura \n8. Ford \n9. Dodge \n10. GNC   Exit\n");
    	scanf("%d", &ch);
    	strcpy(carname,criteriaselector.companyList[ch-1]);
    
    
    	printf("\n\nEnter the car model: ");
    	scanf("%s", carmodel);
    	printf("\n\nEnter the car year: ");
    	scanf("%s", caryear);
    	printf("\n\nEnter the car type: ");
    	scanf("%s", cartype);
    	printf("\n\nEnter the  quantity of models: ");
    	scanf("%d", &carQuantity);
    
    
    	
    	if(start==NULL){
    		
    		start=(struct node *)malloc(sizeof(struct node));
    		strcpy(start->data.carname,carname);
    		strcpy(start->data.carmodel,carmodel);
    		strcpy(start->data.caryear,caryear);
    		strcpy(start->data.cartype,cartype);
    		start->data.quantity=carQuantity;
    		start->prev=NULL;
    		start->next=NULL;
    		
    	}else{
    		
    		ptr=start;
    		start=(struct node *)malloc(sizeof(struct node));
    		strcpy(start->data.carname,carname);
    		strcpy(start->data.carmodel,carmodel);
    		strcpy(start->data.caryear,caryear);
    		strcpy(start->data.cartype,cartype);
    		start->data.quantity=carQuantity;
    		start->next=ptr;
    		
    	}
    	
    }
    
    
    void delete_first(){
    	
    	struct node *ptr;
    	char carname[MAX_WORD_LENGTH];
    	char carmodel[MAX_WORD_LENGTH];
    	char caryear[MAX_WORD_LENGTH];
    	char cartype[MAX_WORD_LENGTH];
    	char modelNumber[MAX_WORD_LENGTH];
    	int  carQuantity;
    	
    	if(start==NULL){
    		printf("\n\nLinked list is empty.\n");
    	}else{
    		ptr=start;
    		printf("\nThe car for which the entry is removed is %s \n",ptr->data.carname);
    		strcpy(start->data.carname,carname);
    		strcpy(start->data.carmodel,carmodel);
    		strcpy(start->data.caryear,caryear);
    		strcpy(start->data.cartype,cartype);
    		start->data.quantity=carQuantity;
    		start=start->next;
    		free(ptr);
    	}
    }
    
    
    void display()
    {
    	struct node *ptr=start;
    	int i=1;
    	
    	if(ptr == NULL){
    		printf("\nLinklist is empty.\n");
    	}else{
    		printf("\nSr. No   Make     Model    Year   Type  Quantity\n");
    		while(ptr != NULL){
    			printf("\n%d.\t%s   %s   %s   %s   %d\n", i,ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->data.cartype,ptr->data.quantity);
    			ptr = ptr->next;
    			i++;
    		}
    		
    	}
    }
    
    
    int main(void)
    {
    	int ch;
    	do 
    	{
    		printf("\n\n\n1. Insert \n2. Delete \n3. Display \n4. Exit\n");
    		printf("\nEnter your choice: ");
    		scanf("%d", &ch);
    		
    		switch(ch)
    		{
    			
    			case 1:
    				insert_first();
    				break;
    			case 2:
    				delete_first();
    				break;	
    			case 3:
    				display();
    				break;
    			case 4:
    				exit(0);											
    			default:
    				printf("\n\nInvalid choice. Please try again. \n");
    				
    		}
    		
    	} while(1);
    	
    	
    	return EXIT_SUCCESS;
    }

  10. #10
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    Bump

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I do not like that you bumped your thread. People will see it, whether you do it or not. Moreover, imagine how the number of posts in every thread would increase, without a reason, if everybody did that.

    Your problem is here:
    Code:
    struct node{
         
        CarData data;
        struct node *next;
        struct node *prev;
    }*start=NULL;                     <---- HERE
    You need to write stuct node *.
    If you want to set your pointer to NULL, I suggest doing that inside main, or inside a function.
    If you want to typedef (I would not recommend it now), you need to write
    Code:
    typedef stuct node * start;
    I feel you need an example to structs. Here you are.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    Registered User
    Join Date
    Jun 2013
    Posts
    20
    This doesn't help me at all.It still gives me same error..

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by i++ View Post
    This doesn't help me at all.It still gives me same error..
    He got the question completely wrong.

    This kind of code is not allowed in structure declarations; delete it:
    Code:
    typedef struct criteriaselector{
         
        const char *companyList[10];
        companyList[0] = "Toyota";
        companyList[1] = "Honda";
        companyList[2] = "Hyundai";
        companyList[3] = "Nissan";
        companyList[4] = "Mitsubishi";
        companyList[5] = "Volksvagon";
        companyList[6] = "Acura";
        companyList[7] = "Ford";
        companyList[8] = "Dodge"
        companyList[9] = "GMC";
    
             
    }CriteriaSelector;
    If you only need one criteria selector object though, may I suggest:

    Code:
    static const CriteriaSelector criteriaselector = { {"Toyota", "Honda", "Hyundai", "Nissan", "Mitsubishi", 
    "Volkwagon", "Acura", "Ford", "Dodge", "GMC"} };
    That will create the sort of object you require.

  14. #14
    Registered User
    Join Date
    Jun 2013
    Posts
    20

    How to give users options based on current selection?

    Thanks whiteflags, I already fixed that solution.Now then,in my program,I plan to filter choices for the users to get a specific piece of data for the inventory. For instance,at start of the program, there will be menu that gives user option to add or delete data and then for instance if they say insert then I will give them option to select a car company(i.e. like Toyota or Honda) and once they select that company I will give them choice of all Toyota models they can add in the inventory.This will happen so I can narrow down and do a selected operation on the information I have been given.


    The thing is I don't know how to filter out the models for a specific company.I have created different arrays containing different models but I don't know how to display user option of models for that company.


    Here is my code..


    Code:
        #include <stdio.h>
        #include <stdlib.h>
        
        #define MAX_WORD_LENGTH 20
        
        #define MAX_SIZE 100
        typedef struct cardata{
            
            char carname[MAX_WORD_LENGTH];
            char carmodel[MAX_WORD_LENGTH];
            char caryear[MAX_WORD_LENGTH];
            char cartype[MAX_WORD_LENGTH];
            int  quantity;
            
        }CarData;
        
        
        struct node{
            
            CarData data;
            struct node *next;
            struct node *prev;
        }*start=NULL;
        
        const char *companyList[10] = {"Toyota", "Honda","Hyundai","Nissan","Mitsubishi","VoksWagon","Acura","Ford","Dodge","GMC"};
        const char *companyModels[10] = {toyotaModels,hondamodels,hyundaimodels,nissanmodels,mitsubishimodels,vokswagonmodels,acuramodels,fordmodels,dodgemodels,gmcmodels};
        const char *toyotaModels[10]={"Corolla","Camery"}
        const char *hondaModels[10]={"Civic","Accord"};
        
        
        void insert_first(){
            struct node *ptr;
            
            char carname[MAX_WORD_LENGTH];
            char carmodel[MAX_WORD_LENGTH];
            char caryear[MAX_WORD_LENGTH];
            char cartype[MAX_WORD_LENGTH];
            int  carQuantity;
            int ch;
            
            printf("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5.Mitsubishi \n6.Volksvagon \n7.Acura \n8.Ford \n9.Dodge \n10.GMC\n");
            printf("\nPress a number to select corresponding car(i.e. 1 for toyota, 2 for honda): ");
            scanf("%d", &ch);
            strcpy(carname,companyList[ch-1]);
            printf("\n\nEnter the car model: ");
            scanf("%s", carmodel);
            printf("\n\nEnter the car year: ");
            scanf("%s", caryear);
            printf("\n\nEnter the car type: ");
            scanf("%s", cartype);
            printf("\n\nEnter the  quantity of models: ");
            scanf("%d", &carQuantity);
        
            
            if(start==NULL){
                
                start=(struct node *)malloc(sizeof(struct node));
                strcpy(start->data.carname,carname);
                strcpy(start->data.carmodel,carmodel);
                strcpy(start->data.caryear,caryear);
                strcpy(start->data.cartype,cartype);
                start->data.quantity=carQuantity;
                start->prev=NULL;
                start->next=NULL;
                
            }else{
                
                ptr=start;
                start=(struct node *)malloc(sizeof(struct node));
                strcpy(start->data.carname,carname);
                strcpy(start->data.carmodel,carmodel);
                strcpy(start->data.caryear,caryear);
                strcpy(start->data.cartype,cartype);
                start->data.quantity=carQuantity;
                start->next=ptr;
                
            }
            
        }
        
        void delete_first(){
            
            struct node *ptr;
            char carname[MAX_WORD_LENGTH];
            char carmodel[MAX_WORD_LENGTH];
            char caryear[MAX_WORD_LENGTH];
            char cartype[MAX_WORD_LENGTH];
            char modelNumber[MAX_WORD_LENGTH];
            int  carQuantity;
            
            if(start==NULL){
                printf("\n\nLinked list is empty.\n");
            }else{
                ptr=start;
                printf("\nThe car for which the entry is removed is %s \n",ptr->data.carname);
                strcpy(start->data.carname,carname);
                strcpy(start->data.carmodel,carmodel);
                strcpy(start->data.caryear,caryear);
                strcpy(start->data.cartype,cartype);
                start->data.quantity=carQuantity;
                start=start->next;
                free(ptr);
            }
        }
        
        void display()
        {
            struct node *ptr=start;
            int i=1;
            
            if(ptr == NULL){
                printf("\nLinklist is empty.\n");
            }else{
                printf("\nSr. No   Make     Model    Year   Type  Quantity\n");
                while(ptr != NULL){
                    printf("\n%d.\t%s   %s   %s   %s   %d\n", i,ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->data.cartype,ptr->data.quantity);
                    ptr = ptr->next;
                    i++;
                }
                
            }
        }
        
        int main(void)
        {
            int ch;
            do 
            {
                printf("\n\n\n1. Insert \n2. Delete \n3. Display \n4. Exit\n");
                printf("\nEnter your choice: ");
                scanf("%d", &ch);
                
                switch(ch)
                {
                    
                    case 1:
                        insert_first();
                        break;
                    case 2:
                        delete_first();
                        break;    
                    case 3:
                        display();
                        break;
                    case 4:
                        exit(0);                                            
                    default:
                        printf("\n\nInvalid choice. Please try again. \n");
                        
                }
                
            } while(1);
            
            
            return 0;
        }
    Last edited by i++; 08-06-2013 at 09:04 PM.

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If it were me I would prepare a smaller array based on the user's choice. To further explain, I would depend a master array of all the CarData from the cars that a user can purchase. From that array, create a smaller array of all the cars that match the make.

    This array can be composed in a loop. I will give you the general idea in pseudocode, but you will have to write it in C.
    Code:
    for each car in inventory:
      if strcmp(car.make, userMake) == 0:
        copy car to filtered array
    It's not the fanciest way, but it works. Once you have that array you can pare it down even further with the user's other specifications by using similar techniques. Or just print out the filtered array with printf().
    Last edited by whiteflags; 08-06-2013 at 09:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have an error that i can't solve
    By mdennis10 in forum C++ Programming
    Replies: 5
    Last Post: 04-15-2011, 11:11 AM
  2. how to solve this error msg
    By rhythm in forum C++ Programming
    Replies: 11
    Last Post: 02-08-2008, 10:05 PM
  3. Cant solve this error... please help...
    By Goosie in forum C++ Programming
    Replies: 16
    Last Post: 07-08-2005, 10:32 PM
  4. please help me to solve the error
    By Jasonymk in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 09:08 AM
  5. ^^ help me solve the error
    By skwei81 in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2003, 09:04 AM

Tags for this Thread