Please help with errors in this code, it would be helpful if you could fix and e
ID: 3813142 • Letter: P
Question
Please help with errors in this code, it would be helpful if you could fix and explain, thanks.
C programming
Start
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct MobileStore
{
char brand[50];
char model[50];
char color[50];
int memory;
float price;
int stock;
struct MobileStore *next;
} mobile;
struct MobileStore *first=NULL,*last=NULL,*k;
char getMenuOption()
{
char o;
while(1)
{
printf(" MENU ");
printf(" Enter C for creating database");
printf(" Enter A for Add new mobile info");
printf(" Enter S for searching a Single mobile info");
printf(" Enter V for view all mobiles in store");
printf(" Enter D for delete a mobile in store");
printf(" Enter x for exit!");
printf(" Enter the choice:");
scanf("%c",&o);
}
return o;
}
int main()
{
char ch;
ch = getMenuOption();
switch(ch)
{
case 'C':
printf(" Enter the number of mobiles you want to enter:");
scanf("%d",&n);
createdatabase(mobile *n);
break;
case 'V':
printMobileInfo();
break;
case 'A':
addNewMobile();
break;
case 'D':
del();
break;
case 'S':
printSingleMobile();
break;
case 'X':
exit(0);
break;
default:
printf(" You have entered a wrong choice!!!");
}
}
getchar();
void createdatabase(mobile *n)
{
int i;
first=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf(" Enter the ",n,"st mobile info:");
printf("Brand : ");
scanf("%s",first->brand);
printf("Model : ");
scanf("%s",first->model);
printf("color : ");
scanf("%s",first->color);
printf("memory : ");
scanf("%d",first->memory);
printf("price : ");
scanf("%d",first->price);
printf("EMI : ");
scanf("%f",first->emi);
printf("stock : ");
scanf("%d",first->stock);
first->next=NULL;
last=first;
for(i=1; i
{
k=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf(" Enter the st mobile info:");
printf("Brand : ");
scanf("%s",k->brand);
printf("Model : ");
scanf("%s",k->model);
printf("color : ");
scanf("%s",k->color);
printf("memory : ");
scanf("%d",k->memory);
printf("price : ");
scanf("%d",k->price);
printf("EMI : ");
scanf("%f",k->emi);
printf("stock : ");
scanf("%d",k->stock);
k->next=NULL;
last->next=k;
last=k;
}
}
void printMobileInfo()
{
struct MobileStore *t;
t=first;
while(t!=NULL)
{
printf(" Brand:%s",t->brand);
printf(" Model:%s",t->model);
printf(" color:%s",t->color);
printf(" Memory :%s",t->memory);
printf(" price:%s",t->price);
printf(" EMI:%s",t->emi);
printf(" Stock:%s",t->stock);
t=t->next;
}
}
void addNewMobile()
{
char r[10];
int flag=0;
printf(" Enter mobile model insert mobile after that:");
scanf("%s",r);
struct MobileStore *t;
t=first;
while(t!=NULL)
{
if(strcmpi(r,t->model)==0)
{
k=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf("Brand : ");
scanf("%s",k->brand);
printf("Model : ");
scanf("%s",k->model);
printf("color : ");
scanf("%s",k->color);
printf("memory : ");
scanf("%d",k->memory);
printf("price : ");
scanf("%d",k->price);
printf("EMI : ");
scanf("%f",k->emi);
printf("stock : ");
scanf("%d",k->stock);
k->next=t->next;
t->next=k;
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf(" The element not found!!!");
}
void del()
{
struct MobileStore *back,*t,*k;
char r[10];
int flag=0;
printf(" Enter the model number u wanna delete:");
scanf("%s",r);
if(strcmpi(r,first->model)==0)
{
first=first->next;
flag=1;
}
else
{
back=first;
k=first->next;
while(k!=NULL)
{
if(strcmpi(r,k->model)==0)
{
back->next=k->next;
flag=1;
break;
}
}
}
if(flag==0)
printf(" The element not found!!!");
}
void printSingleMobile()
{
char r[10];
int flag=0;
printf(" Enter the model number u wanna search:");
scanf("%s",r);
struct MobileStore *t;
t=first;
while(t!=NULL)
{
if(strcmpi(r,t->model)==0)
{
printf(" The model number found in the list!!! brand name is %s",t->brand);
printf("model name is %s",t->model);
printf("color is %s",t->color);
printf("memory size is %s",t->memory);
printf("Price is %s",t->price);
printf("EMI is %s",t->emi);
printf("Stock available is %s",t->stock);
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf(" The model number not in database!!");
}
Explanation / Answer
Following is the working program
#include<stdio.h>
#include<stdlib.h>
//#include<conio.h>
#include <string.h>
struct MobileStore
{
char brand[50];
char model[50];
char color[50];
int memory;
float price;
int stock;
struct MobileStore *next;
} mobile;
struct MobileStore *first=NULL,*last=NULL,*k;
void printSingleMobile();
char getMenuOption()
{
char o;
while(1)
{
printf(" MENU ");
printf(" Enter C for creating database");
printf(" Enter A for Add new mobile info");
printf(" Enter S for searching a Single mobile info");
printf(" Enter V for view all mobiles in store");
printf(" Enter D for delete a mobile in store");
printf(" Enter x for exit!");
printf(" Enter the choice:");
scanf("%c",&o);
}
return o;
}
void createdatabase(unsigned int n)
{
int i = 1;
first=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf(" Enter the ",i,"st mobile info:");
printf("Brand : ");
scanf("%s",first->brand);
printf("Model : ");
scanf("%s",first->model);
printf("color : ");
scanf("%s",first->color);
printf("memory : ");
scanf("%d",first->memory);
printf("price : ");
scanf("%d",first->price);
//printf("EMI : ");
//scanf("%f",first->emi);
printf("stock : ");
scanf("%d",first->stock);
first->next=NULL;
last=first;
for(i=2; i <= n ;i++)
{
k=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf(" Enter the st mobile info:");
printf("Brand : ");
scanf("%s",k->brand);
printf("Model : ");
scanf("%s",k->model);
printf("color : ");
scanf("%s",k->color);
printf("memory : ");
scanf("%d",k->memory);
printf("price : ");
scanf("%d",k->price);
//printf("EMI : ");
//scanf("%f",k->emi);
printf("stock : ");
scanf("%d",k->stock);
k->next=NULL;
last->next=k;
last=k;
}
}
void printMobileInfo()
{
struct MobileStore *t;
t=first;
while(t!=NULL)
{
printf(" Brand:%s",t->brand);
printf(" Model:%s",t->model);
printf(" color:%s",t->color);
printf(" Memory :%s",t->memory);
printf(" price:%s",t->price);
//printf(" EMI:%s",t->emi);
printf(" Stock:%s",t->stock);
t=t->next;
}
}
void addNewMobile()
{
char r[10];
int flag=0;
printf(" Enter mobile model insert mobile after that:");
scanf("%s",r);
struct MobileStore *t;
t=first;
while(t!=NULL)
{
if(strcmp(r,t->model)==0)
{
k=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf("Brand : ");
scanf("%s",k->brand);
printf("Model : ");
scanf("%s",k->model);
printf("color : ");
scanf("%s",k->color);
printf("memory : ");
scanf("%d",k->memory);
printf("price : ");
scanf("%d",k->price);
//printf("EMI : ");
//scanf("%f",k->emi);
printf("stock : ");
scanf("%d",k->stock);
k->next=t->next;
t->next=k;
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf(" The element not found!!!");
}
void del()
{
struct MobileStore *back,*t,*k;
char r[10];
int flag=0;
printf(" Enter the model number u wanna delete:");
scanf("%s",r);
if(strcmp(r,first->model)==0)
{
first=first->next;
flag=1;
}
else
{
back=first;
k=first->next;
while(k!=NULL)
{
if(strcmp(r,k->model)==0)
{
back->next=k->next;
flag=1;
break;
}
}
}
if(flag==0)
printf(" The element not found!!!");
}
void printSingleMobile()
{
char r[10];
int flag=0;
printf(" Enter the model number u wanna search:");
scanf("%s",r);
struct MobileStore *t;
t=first;
while(t!=NULL)
{
if(strcmp(r,t->model)==0)
{
printf(" The model number found in the list!!! brand name is %s",t->brand);
printf("model name is %s",t->model);
printf("color is %s",t->color);
printf("memory size is %s",t->memory);
printf("Price is %s",t->price);
//printf("EMI is %s",t->emi);
printf("Stock available is %s",t->stock);
flag=1;
break;
}
t=t->next;
}
if(flag==0)
printf(" The model number not in database!!");
}
int main()
{
char ch;
while (1) {
ch = getMenuOption();
unsigned int n = 0;
switch(ch)
{
case 'C':
printf(" Enter the number of mobiles you want to enter:");
scanf("%d",&n);
createdatabase(n);
break;
case 'V':
printMobileInfo();
break;
case 'A':
addNewMobile();
break;
case 'D':
del();
break;
case 'S':
printSingleMobile();
break;
case 'X':
exit(0);
break;
default:
printf(" You have entered a wrong choice!!!");
}
}
getchar();
}
To view the change, you can use some tool like cleardiff.
Still, if you face any issue,then please let me know.
Thanks