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++;
}
}
}
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++;
}
}
}
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;
}
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;
}
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;
}