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

C programming I have this program close to be done but I am stuck with a constan

ID: 3814425 • Letter: C

Question

C programming

I have this program close to be done but I am stuck with a constant loop inside main. Please help, with warnings as well. attatched is the following code.

please actually test your code before to confirm as well, thanks.

Begin Code

#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();

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("%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 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("%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:%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();
}

The purpose of this project is to create, view, update, and delete a database of records using a linked Consider a store where different brands of mobile phones are sold. The details of the phones in the store are read from keyboard (stdin). You are required to perform the following operations 1) Create Read in mobile phone data from stdin into a linked list. 2) View Display contents of the linked list 3) View Print a single mobile details by reading the model of a phone 4) Update Add a new phone data Read data from stdin into a new node and insert the node into the existing list. 5) Delete Erase all the data stored in the dynamic memory Before writing the main function, create a MobileStore Structure as follows typedef struct Mobilestore f char brand [50] char model 50] char color [50] int memory; float price; float emi; int stock; struct Mobile Store next mobile Create the following functions to complete the project. int main Call the getMenuoption() function to print available options Provide a switch statement to call the appropriate function with the selected choice. If invalid choice is selected, an error message should be displayed char get Menuoption0, Display the menu options (check the sample output for the menu). Read the option selected by the user and return it to the main function. mobile readMobiles(mobile head); Prompt the user to enter the number of mobiles. n this function, read data provided by the user to create a linked list in memory. An empty input line should end input from the user

Explanation / Answer

I have resolved few issues in your program:-

1. There was a while loop inside getMenuOption function which was resulting in your menu displayed again and again. I removed that while loop. then menu started woirking.

2. On click of any option and then enter, it was storing the option as well as subsequent enter character ' ' in the buffer, which I needed to clear manually. So added function clearBuffer() and called it on each menu item selection.

Code is as below: -

#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 ",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("%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 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("%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:%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);
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();
}