Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming ! - implement fgets instead scanf(\"%s\",&new_node->car_name); int

ID: 3576660 • Letter: C

Question

C programming ! - implement fgets instead scanf("%s",&new_node->car_name); into my program.

Source code:

#include<stdio.h>
#include<stdlib.h>

struct node
{
char car_name[20];
char type;
int model_year;
float car_val;
float sale_price;
struct node *next;
};


void add(struct node** head_ref)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
struct node *last=*head_ref;
int year;
float val,saleprice;

printf(" Enter car name: ");
scanf("%s",&new_node->car_name);

printf("Enter car type: ");
scanf(" %c",&new_node->type);

printf("Enter model year: ");
scanf("%d",&year);
new_node->model_year=year;
printf("Enter car value: ");
scanf("%f",&val);
new_node->car_val=val;
printf("Enter car sales price: ");
scanf("%f",&saleprice);
new_node->sale_price=saleprice;
new_node->next=NULL;
if(*head_ref==NULL)
{
  *head_ref=new_node;
  return;
}
while(last->next!=NULL)
{
  last=last->next;
}
last->next=new_node;
return;
}


void deleteCar(struct node**head_ref)
{
char name[20];
printf(" Enter car info: ");
printf(" Enter car name: ");
scanf("%s",&name);

struct node *temp=*head_ref,*prev;
if(temp!=NULL && strcmp(temp->car_name,name)==0)
{
  *head_ref=temp->next;
  free(temp);
  return;
}
while(temp!=NULL && strcmp(temp->car_name,name)!=0)
{
  prev=temp;
  temp=temp->next;
}

if(temp==NULL)
return;
prev->next=temp->next;
free(temp);
}


void printList(struct node * node)
{
if(node==NULL)
printf("No cars added yet.");
while(node!=NULL)
{
  printf(" Car name: %s ",node->car_name);
  printf(" Car type: %c ",node->type);
  printf(" Car model year: %d ",node->model_year);
  printf(" Car value: %f ",node->car_val);
  printf(" Car sales price: %f ",node->sale_price);
  
  node=node->next;
}
}

int main()
{
int op,i=0;
struct node* head=NULL;

while(i!=1)
{
    printf(" 1. See a car's information 2. Add a car 3. Remove a car 4. Generate Report. 5. Exit ");
    printf("Enter your choice: ");
    scanf("%d",&op);
    switch(op)
    {
      case 1:
       printf("Enter car info: ");
       break;
      case 2:
       add(&head);
       break;
      case 3:
       deleteCar(&head);
       break;
      case 4:
       printf("Generating report.....");
       printList(head);
       break;
      case 5:
       i=1;
       break;
      default:
       printf("Enter right option!");
       break;
  }

    }
    return 0;
}

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char car_name[20];
char type;
int model_year;
float car_val;
float sale_price;
struct node *next;
};

void add(struct node** head_ref)
{
struct node* new_node=(struct node*)malloc(sizeof(struct node));
struct node *last=*head_ref;
int year;
float val,saleprice;
printf(" Enter car name: ");
fgets(new_node->car_name,30,stdin);
printf("Enter car type: ");
scanf(" %c",&new_node->type);
printf("Enter model year: ");
scanf("%d",&year);
new_node->model_year=year;
printf("Enter car value: ");
scanf("%f",&val);
new_node->car_val=val;
printf("Enter car sales price: ");
scanf("%f",&saleprice);
new_node->sale_price=saleprice;
new_node->next=NULL;
if(*head_ref==NULL)
{
*head_ref=new_node;
return;
}
while(last->next!=NULL)
{
last=last->next;
}
last->next=new_node;
return;
}

void deleteCar(struct node**head_ref)
{
char name[20];
printf(" Enter car info: ");
printf(" Enter car name: ");
scanf("%s",&name);

struct node *temp=*head_ref,*prev;
if(temp!=NULL && strcmp(temp->car_name,name)==0)
{
*head_ref=temp->next;
free(temp);
return;
}
while(temp!=NULL && strcmp(temp->car_name,name)!=0)
{
prev=temp;
temp=temp->next;
}

if(temp==NULL)
return;
prev->next=temp->next;
free(temp);
}

void printList(struct node * node)
{
if(node==NULL)
printf("No cars added yet.");
while(node!=NULL)
{
printf(" Car name: %s ",node->car_name);
printf(" Car type: %c ",node->type);
printf(" Car model year: %d ",node->model_year);
printf(" Car value: %f ",node->car_val);
printf(" Car sales price: %f ",node->sale_price);
  
node=node->next;
}
}
int main()
{
int op,i=0;
struct node* head=NULL;

while(i!=1)
{
printf(" 1. See a car's information 2. Add a car 3. Remove a car 4. Generate Report. 5. Exit ");
printf("Enter your choice: ");
scanf("%d",&op);
switch(op)
{
case 1:
printf("Enter car info: ");
break;
case 2:
add(&head);
break;
case 3:
deleteCar(&head);
break;
case 4:
printf("Generating report.....");
printList(head);
break;
case 5:
i=1;
break;
default:
printf("Enter right option!");
break;
}
}
return 0;
}