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

Any help on this would be great.. I have absoulety no idea where to start. This

ID: 3821487 • Letter: A

Question

Any help on this would be great.. I have absoulety no idea where to start. This is specifically for only C programming. (Not C++). Thank you in advance!!

7
Apple   iphone-6s   gray   64   799.99   15.54   5
Apple iphone-6splus silver   128   899.00   18.33   4
Apple   iphone-7   black   32   899.99   18.75   5
Apple   iphone-7plus   silver   128   999.99   29.04   3
Samsung   Galaxy-s7   silver   32   799.99   24.99   4
Samsung   Galaxy-s8   black   64   849.99   36.99   10
LG   G5       Black   32   599.99   9.99   7  

This is what the text file we are supposed to upload looks like. I have absoulety no idea what to do.

The purpose of this project is to create, view, update and delete a database using linked lists and FILE I/O operations Consider a mobile store where different brands of mobiles are sold. The details of the mobiles present in the store are given in the "in txt file which is present along with this document. put You are required to perform the following operations: 1) Load the mobile database Read and store all the data from the input txt file to dynamic memory. 2) View all mobiles data After loading the database, print all the mobile details from the input text file. Without loading the database, you cannot view the mobiles info. 3) Add a new mobile Add a new mobile with its specifications and make sure it is saved. In order to check this, repeat the view all mobiles' option and see if the new mobile is added to the existing database. 4) Free database This function will clear all the information from the database and ends the program Before writing the main function, create a MobileStore Structure as shown below. typedef struct Mobilestore char brand [50 char model 1501; char color [50] int memory; float price; float emi int stock; struct Mobile Store next 1 mobile Create the below functions and follow the guidelines to complete the project. int main Call the getMenuoption0 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 getMenuoption0 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.

Explanation / Answer

Hi, you can start by creating the struct for reading each record of the input file and then creating a linked list for storing the read records. Below is a rough code to help you get started.

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

/*this struct will hold each record in the database this program will create.
In other words, this is the structure of each record in the database*/
typedef struct MobileStore{
char brand[50];
char model[50];
char color[50];
int memory;
float price;
float emi;
int stock;
struct MobileStore *next; /*pointer to the next record in the database (linked list)*/
}mobile; /*an instance of the above struct*/
mobile *head;/*starting node of the linked list*/
mobile *current;/*current node of the linked list*/

void readMobiles()/*read from the input file and create a linked list of the records read
This will be the database*/
{
int NoOfRecords=0; /*no of records in the file, this will be the length of the linked list*/
int i; /*counter for loop*/
FILE *fp=fopen("input.txt","r"); /*open the input file for reading*/

if(!fp) /*if unable to open file for some reason*/
{
printf("Error in reading the file!");
exit(1);
}
fscanf(fp,"%d",&NoOfRecords); /* the first piece of data in the file is the number of records,
read this into the NoOfRecords variable*/
head=malloc(sizeof(mobile)); /*allocate memory to head*/

current=head; /*in the beginning head is the current node*/
for(i=0;i<NoOfRecords;i++) /*read all records*/
/*read each field of the record; each record has 7 fields*/
{
fscanf(fp,"%s",&(current->brand));
fscanf(fp,"%s",&(current->model));
fscanf(fp,"%s",&(current->color));
fscanf(fp,"%d",&(current->memory));
fscanf(fp,"%f",&(current->price));
fscanf(fp,"%f",&(current->emi));
fscanf(fp,"%d",&(current->stock));
if(i<NoOfRecords-1)
{
current->next=malloc(sizeof(mobile));/*allocate memory for the next record and link it to the current record*/
current=current->next; /*go to the next record*/
}
else
current->next=NULL; /*end of linked list*/
}
fclose(fp); /*close the input file*/
}/*end of readmobiles()*/

void printMobileInfo() /*print all data onto the screen*/
{
current=head; /*assign the start of the database to current*/
while(current) /*loop as long as current != NULL*/
{
printf(" Brand : %s",current->brand);
printf(" Model : %s",current->model);
printf(" Color : %s",current->color);
printf(" Memory : %d",current->memory);
printf(" Price : %f",current->price);
printf(" Emi Price : %f",current->emi);
printf(" Stock : %d ",current->stock);
current=current->next; /*go to the next record*/
}
}

void addNewMobile() /*add new mobile to database*/
{
mobile* newrecord=malloc(sizeof(mobile)); /*allocate memory for new record*/
/*get the new data*/
fflush(stdin);
printf(" Enter the Brand Name : ");
gets(newrecord->brand);
printf(" Enter the Model Name : ");
gets(newrecord->model);
printf(" Enter the Color : ");
gets(newrecord->color);
fflush(stdin);
printf(" Enter the Memory : ");
scanf("%d",&(newrecord->memory));
printf(" Enter the Price : ");
scanf("%f",&(newrecord->price));
printf(" Enter the EMI Price : ");
scanf("%f",&(newrecord->emi));
printf(" Enter the Stock : ");
scanf("%d",&(newrecord->stock));
newrecord->next=NULL;

/*navigate to the end of the current list and add the new record to it*/
current=head;
while(current->next!=NULL) /*until current->next==NULL*/
current=current->next;
current->next=newrecord; /*append the new record to the list*/
}

void printSingleMobile()
{
char* ModelNo;
int found=0; /*set this to 1 if model found in database*/

printf("Enter the Model No to find : ");
fflush(stdin);
gets(ModelNo);
current=head;
/*loop until model found in data base or end of data base*/

while(current!=NULL)
{
if(strcmp(ModelNo,(current->model))==0)
{
found=1; /*set found to 1*/
break;
}
current=current->next;
}
printf("%d ",found);
if(found==1) /*if model found*/
{
printf(" Brand : %s",current->brand);
printf(" Model : %s",current->model);
printf(" Color : %s",current->color);
printf(" Memory : %d",current->memory);
printf(" Price : %f",current->price);
printf(" Emi Price : %f",current->emi);
printf(" Stock : %d ",current->stock);
}
else
printf("Model Not found. ");
}

void freeDatabase() /*delete the database*/
{
mobile *temp;

printf("nFreeing Memory... ");
head=current;
while(current)
{
temp=current->next;
free(current);
current=temp;
}
head=NULL;
printf(" Exiting... ");
exit(0);
}

char getMenuOption()
{
char choice;

printf("Mobile Database System: ");
printf(" L - Load the Database");
printf(" A - Add new mobile info");
printf(" S - Search single mobile info");
printf(" V - View all mobiles in store");
printf(" X - Exit ");
printf(" Enter a choice :");
fflush(stdin);
scanf("%c",&choice);

return choice;
}
int main()
{
char choice;
choice=getMenuOption();
choice=toupper(choice);
while(choice!='X')
{
switch(choice)
{
case 'L' : if(head==NULL) /*if not already loaded*/
readMobiles();
else
{
printf("Database already loaded ");
}
choice=toupper(getMenuOption());
break;
case 'A' : addNewMobile();
choice=toupper(getMenuOption());
break;
case 'S' : printSingleMobile();
choice=toupper(getMenuOption());
break;
case 'V' : printMobileInfo();
choice=toupper(getMenuOption());
break;
case 'X' : freeDatabase();
break;
default : printf("Invalid entry.");
choice=toupper(getMenuOption());
}
}
freeDatabase(); /*this is repeated here as it may not get called after the while loop*/
return 0;
}

Hope this helps!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote