My current code, please use. start #include <stdio.h> #include <stdlib.h> #inclu
ID: 3815617 • Letter: M
Question
My current code, please use.
start
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
typedef struct MobileStore
{
char brand[50];
char model[50];
char color[50];
int memory;
float price;
float emi;
int stock;
struct MobileStore *next;
} mobile;
struct MobileStore *first=NULL,*last=NULL,*k;
void printSingleMobile();
void clearBuffer(char c)
{
while(c != ' ')
{
scanf("%c", &c);
}
}
char getMenuOption()
{
char o;
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);
clearBuffer(o);
return o;
}
void createdatabase(unsigned int n)
{
int i = 1;
first=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf(" Enter the mobile 1 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("%f", &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 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("%f", &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 :%d", t->memory);
printf(" Price:%f",t->price);
printf(" EMI:%f",t->emi);
printf(" Stock:%d ",t->stock);
t=t->next;
}
printf(" Press enter to continue..");
}
void addNewMobile()
{
char r[10];
int flag=0;
printf(" Enter new mobile model:");
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("%f", &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,*k;
char r[10];
int flag=0;
printf(" Enter the model name to 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 to 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 %d", t->memory);
printf("Price is %f",t->price);
printf("EMI is %f",t->emi);
printf("Stock available is %d",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);
clearBuffer(ch);
break;
case 'V':
printMobileInfo();
clearBuffer(ch);
break;
case 'A':
addNewMobile();
clearBuffer(ch);
break;
case 'D':
del();
clearBuffer(ch);
break;
case 'S':
printSingleMobile();
clearBuffer(ch);
break;
case 'X':
exit(0);
break;
default:
printf(" You have entered a wrong choice!!!");
}
}
getchar();
}
Explanation / Answer
NOTE : Your logic for del() and addNewMobile method was wrong. So I corrected it. It works perfectly fine now
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <string.h>
typedef struct MobileStore
{
char brand[50];
char model[50];
char color[50];
int memory;
float price;
float emi;
int stock;
struct MobileStore *next;
} mobile;
struct MobileStore *first=NULL,*last=NULL,*k;
void printSingleMobile();
void clearBuffer(char c)
{
while(c != ' ')
{
scanf("%c", &c);
}
}
char getMenuOption()
{
char o;
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);
clearBuffer(o);
return o;
}
void createdatabase(unsigned int n)
{
int i = 1;
first=(struct MobileStore*)malloc(sizeof(struct MobileStore));
printf(" Enter the mobile 1 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("%f", &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 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("%f", &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 :%d", t->memory);
printf(" Price:%f",t->price);
printf(" EMI:%f",t->emi);
printf(" Stock:%d ",t->stock);
t=t->next;
}
printf(" Press enter to continue..");
}
void addNewMobile()
{
char r[10];
printf(" Enter new mobile model:");
scanf("%s",r);
struct MobileStore *t;
t=first;
while(t!=NULL)
{
if(strcmp(r,t->model)==0)
{
printf(" Mobile Already Exists!!!");
return;
}
if (t->next == NULL)
break;
t=t->next;
}
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("%f", &k->price);
printf("EMI : ");
scanf("%f", &k->emi);
printf("stock : ");
scanf("%d", &k->stock);
t->next=k;
}
void del()
{
struct MobileStore *back,*k;
char r[10];
int flag=0;
printf(" Enter the model name to 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;
}
back = k;
k = k->next;
}
}
if(flag==0)
printf(" The element not found!!!");
}
void printSingleMobile()
{
char r[10];
int flag=0;
printf(" Enter the model number to 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 %d ", t->memory);
printf("Price is %f ",t->price);
printf("EMI is %f ",t->emi);
printf("Stock available is %d ",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);
clearBuffer(ch);
break;
case 'V':
printMobileInfo();
clearBuffer(ch);
break;
case 'A':
addNewMobile();
clearBuffer(ch);
break;
case 'D':
del();
clearBuffer(ch);
break;
case 'S':
printSingleMobile();
clearBuffer(ch);
break;
case 'X':
exit(0);
break;
default:
printf(" You have entered a wrong choice!!!");
}
}
getchar();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.