Question
Hi. I made a code but I have to change it. These are the requirements for the program code Safe entry No magic numbers No
Hi. I made a code but I have to change it. These are the requirements for the program code Safe entry No magic numbers No global variables File management must be in a separate c-file - I HAVEN'T DONE THIS AND I NEED HELP All standard header files, type declarations, function prototypes etc. must be included a custom header file - I HAVEN'T DONE THIS EITHER Functions must not be too long
and when the user chooses to quit "exit", the register must be written to a file - i haven't done this
So basically I need to change the code so that it fits the requirements
#include
#include
#include
#include
#define STR_LANGD 30
#define VEHICLE 10
#define NUMBEROF_PEOPLE 10
typedef struct
{
char name[STR_LANGD];
int age;
}
Person;
typedef struct
{
Person person;
char model[STR_LANGD];
char carbrand[STR_LANGD];
char registrationnumber[STR_LANGD];
}
Vehicle;
void initVehicle(Vehicle myVehicle[])
{
int i;
for (i = 0; i < VEHICLE; i++)
{
myVehicle[i].person.age = 0;
}
}
int menu() {
int choice = 0;
printf ("1. Add a vehicle. ");
printf ("2. Remove a vehicle ");
printf ("3. Sort by car brand. ");
printf ("4. Print vehicle information. ");
printf ("5. Print the entire vehicle register. ");
printf ("0. Exit. ");
scanf("%d", &choice);
return choice;
}
void AddVehicle(Vehicle myVehicle[])
{
Vehicle temp;
int i;
fflush(stdin);
printf("Enter name: ");
scanf("%s", temp.person.name);
printf("Enter personens lder: ");
scanf("%d", &temp.person.age);
printf("Enter Vehiclestyp: ");
scanf("%s", temp.model);
printf("Enter car brand: ");
scanf("%s", temp.carbrand);
printf("Enter registration number: ");
scanf("%s", temp.registrationnumber);
for (i = 0; i < VEHICLE; i++)
{
if (myVehicle[i].person.age == 0)
{
myVehicle[i] = temp;
break;
}
}
}
void showVehicle(Vehicle myVehicle[])
{
int i;
printf(" All vehicle in database ");
printf("------------------- ");
for (i = 0; i < VEHICLE; i++)
{
if (myVehicle[i].person.age != 0)
{
printf("Place: %d ", i);
printf("Name: ");
puts(myVehicle[i].person.name);
printf("lder p garen: %d ", myVehicle[i].person.age);
printf("Car model: %s ", myVehicle[i].model);
printf("Car brand: %s ", myVehicle[i].carbrand);
printf("Registration number: %s ", myVehicle[i].registrationnumber);
}
}
}
void DeleteVehicle(Vehicle myVehicle[VEHICLE])
{
int temp;
printf("P vilken plats (0-10) vill du ta bort Vehicleinfo? Plats: ");
scanf("%d", &temp);
if (myVehicle[temp].person.age != 0)
{
myVehicle[temp].person.age = 0;
}
}
void printinfo(Vehicle myVehicle[VEHICLE])
{
int temp;
printf("P vilken plats (0-10) vill du se Vehicleinfo? Plats: ");
scanf("%d", &temp);
if (myVehicle[temp].person.age != 0)
{
printf(" ------------Vehicleinfo----------- ");
printf("Plats: %d", temp);
printf("name: %s ", myVehicle[temp].person.name);
printf("lder p garen: %d ", myVehicle[temp].person.age);
printf("Car model: %s ", myVehicle[temp].model);
printf("Car brand: %s ", myVehicle[temp].carbrand);
printf("Registration number: %s ", myVehicle[temp].registrationnumber);
}
}
void carSort(Vehicle myVehicle[VEHICLE])
{
int i;
int j;
Vehicle temp;
for (i = 1; i < VEHICLE; i++)
{
for (j = 0; j < VEHICLE - i; j++)
{
if (myVehicle[j].carbrand[0] > myVehicle[j+1].carbrand[0])
{
temp = myVehicle[j];
myVehicle[j] = myVehicle[j+1];
myVehicle[j+1] = temp;
}
}
}
}
int main() //main: hr startar alla C-program
{
Vehicle myVehicle[VEHICLE];
int menuchoice = 0;
initVehicle(myVehicle);
do
{
menuchoice = menu();
switch (menuchoice)
{
case 1: AddVehicle(myVehicle); break;
case 2: DeleteVehicle(myVehicle); break;
case 3: carSort(myVehicle); break;
case 4: printinfo(myVehicle); break;
case 5: showVehicle(myVehicle); break;
default: break;
}
}
while (menuchoice !=0);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started