[Solved]Need Help Completing C Program Program Include Loop Allow User Repeat Program Program Must Q37122207
Need help with completing this C program. Program will include aloop and allow the user to repeat the program. Program must includepointer notation, fgets and lists.
Sequence of options:
-Populate existing trucks …this is done once as existing trucksare given (see populate trucks function)
-start loop, display menu (switch case must be used)
-code the necessary option based on user choice:
—if choice is 1, call the addTruck function, validate all inputaccordingly, then call the generate miles(see add truckfunction
remember that you need to save all trucks in a listand not an array of objects
—if choice is 2, prompt for a truck ID, search for it and iffound display all info about the truck and if the truck is to beauctioned or not
must be displayed based on the status
remeber that you need to process your search using the list
—if choice is 3, display all trucks that are to be auctioned, inthis case, check the status of the truck and if true, display thegiven truck
—if choice is 4, quit program with the right message.
IMPORTANT! WHEN USER DECIDES TO QUIT WRITE ALL RECORDS FROM THELIST TO DISK AND THEN READ THEM BACK INTO A NEW LIST FORVALIDATION
These are the functions I have so far (they program is notworking properly)
//======================================================================
int menu()
{
int choice = 0;
printf( “-1-Add new truckn”
“-2-Search a truckn”
“-3-Generate a list of trucks to be auctionedn”
“-4-Quit Programn”
“===>Enter choice: “);
scanf(“%d”, &choice);
return choice;
}//end menu
//=======================================================================
void populateTrucks(truck* pTruck, int truckCount)
{
strcpy((pTruck + 0)->truckID, “01”);
strcpy((pTruck + 0)->truckMake,”Volvo”);
(pTruck + 0)->milesDriven = 200000;
(pTruck + 0)->status = false;
//————————————
strcpy((pTruck + 1)->truckID, “02”);
strcpy((pTruck + 1)->truckMake,”Peterbuilt”);
(pTruck + 1)->milesDriven = 850000;
(pTruck + 1)->status = true;
//————————————
strcpy((pTruck + 2)->truckID, “03”);
strcpy((pTruck + 2)->truckMake,”Mack”);
(pTruck + 2)->milesDriven = 30000;
(pTruck + 2)->status = false;
//————————————
strcpy((pTruck + 3)->truckID, “04”);
strcpy((pTruck + 3)->truckMake,”Volvo”);
(pTruck + 3)->milesDriven = 1000000;
(pTruck + 3)->status = true;
}//end populateDest
//=======================================================================
void addTruck(truck* pTruck, int totalTruck)
{
char input[buffer] = ” “;
char* pinput = NULL;
fflush(stdin);
printf(“Enter the Truck ID: “);
pinput = fgets(input, buffer, stdin);
strtok(pinput, “n”);
//validate
strcpy((pTruck + totalTruck)->truckID,pinput);
printf(“Enter the Truck Make: “);
pinput = fgets(input, buffer, stdin);
//validate
strtok(pinput, “n”);
strcpy((pTruck + totalTruck)->truckMake,pinput);
generateRandomMiles();
}//end addTruck
//=======================================================================
char* getTruckID()
{
char input[buffer]; //buffer is 256 char
char* pinput = NULL; // = &input[0];
fflush(stdin); //rewind()
printf(“Enter the Truck ID: “);
pinput = fgets(input, buffer, stdin);
strtok(pinput, “n”);
return pinput;
}//end getTruckID
//========================================================================
truck* searchTruck(char* ptruckID, truck* phead, int totalTruck,bool* pfound)
{ truck* ptraverse =NULL;
if(strcmp(ptruckID,phead->truckID) == 0)
{ *pfound = true;
return phead;
}//end if
else //not needed becauseof the first return
{ ptraverse = phead;
while(ptraverse->pnext != NULL)
{
ptraverse = ptraverse->pnext;
if(strcmp(ptruckID, ptraverse->truckID) == 0)
{ *pfound = true;
return ptraverse;
}//end if
}//end while
}//end else
pfound = false;
}//end searchTruck
//========================================================================
void dispTruck(truck* pcurrent)
{
printf(“=============================n”);
printf(” Truck ID: %sn”
” Truck Make: %sn”
” Miles Driven: %dn”
” status: %dn”, pcurrent->truckID, pcurrent->truckMake,pcurrent->milesDriven, pcurrent->status);
}//end dispTruck
//=========================================================================
void generateRandomMiles(truck* pcurrent)
{
//generate a random int between 0 and1000000
*****NEED HELPWITH THIS******
GENERATE RANDOM NUMBER BETWEEN 0 AND1000000
}//end generateRandomMiles
//==========================================================================
<replace with function type> bubbleSortTrucks(<insertany needed parametrs…if necessary>)
{
//sort truck by miles
*****NEED HELP WITH THIS******
DORT USING BUBBLE SORT METHOD
}//end sortDestinations
//==========================================================================
<replace with function type> searchBinaryTrucks(<insertany needed parametrs…if necessary>)
{
*****NEED HELP WITH THIS******
SEARCH USING BINARY SEARCH METHOD
}//end sortDestinations
//==========================================================================
void writeTrucks(truck* phead)
{
FILE* pwrite = NULL;
truck* ptraverse = NULL;
pwrite =fopen(“Trucks”, “w”);
if(pwrite == NULL)
{
printf(“Cannot openthis file for writing…any key to continuen”);
getch();
}//end if
else
{
ptraverse =phead;
fwrite(ptraverse,sizeof(truck), 1, pwrite);
while(ptraverse->pnext != NULL)
{ ptraverse =ptraverse->pnext;
fwrite(ptraverse, sizeof(truck), 1, pwrite);
}//end while
}//end else
fclose(pwrite);
}//end writeTrucks
//————————————————————————-
void readTrucks()
{FILE* pread = NULL;
truck* trurecord = NULL;
pread = fopen(“students”, “r”);
if(pread == NULL)
{ printf(“Cannot open this file for reading…any key tocontinuen”);
getch();
}//end if
else
{ int i = 0;
while(i < 50)
{ fread(trurecord, sizeof(truck), 1,pread);
dispTruck(trurecord);
i++;
}
}
fclose(pread);
}//end readTrucks
*****Not sure it writing to a new list****
Expert Answer
Answer to Need help with completing this C program. Program will include a loop and allow the user to repeat the program. Program … . . .
OR

