Hi! can someone help me make this code compile? I think i\'m missing some connec
ID: 3706801 • Letter: H
Question
Hi! can someone help me make this code compile? I think i'm missing some connections. Any help is appreciated :)
#include <stdio.h>
#include <string.h>
typedef struct Car {
int carId;
char make[20];
char model[20];
int numDoors;
double rate;
}Car;
typedef struct Rental{
char renterName[20];
int daysRenting;
int carId;
}Rental;
int createInventory(Car *allCars) {
//car1
allCars[0].carId = 1234;
strcpy(allCars[0].make, "vw");
strcpy(allCars[0].model, "Golf");
allCars[0].numDoors= 2;
allCars[0].rate = 66;
//car2
allCars[1].carId = 2241;
strcpy(allCars[1].make, "Ford");
strcpy(allCars[1].model, "Focus");
allCars[1].numDoors= 4;
allCars[1].rate = 45;
//car3
allCars[2].carId = 3445;
strcpy(allCars[2].make, "bmw");
strcpy(allCars[2].model, "x3");
allCars[2].numDoors= 4;
allCars[2].rate = 128;
return 3;
}
int addNewCar(Car *allCars, int totalCars, int size) {
printf("Enter number of cars to add: ");
scanf("%d", &totalCars);
int i=1;
while(i <= totalCars)
{
if(i == 20) {
printf("Max number of cars reached");
break;
}
Car newCar;
printf("Enter car ID: ");
scanf("%d", &newCar.carId);
printf("Enter car make: ");
scanf("%s", newCar.make);
printf("Enter car model: ");
scanf("%s", newCar.model);
printf("Enter number of car doors: ");
scanf("%d", &newCar.numDoors);
printf("Enter car rating: ");
scanf("%lf", &newCar.rate);
if(size < 20)
allCars[size + 1] = newCar;
size = size + 1;
i = i + 1;
return totalCars;
}
}
int addNewRental(Car *allCars, int totalCars, Rental *allRentals, int totalRentals, int size) {
int i=0;
while (totalRentals <= 60) {
Rental rentNewCar;
printf("Enter renter name: ");
scanf("%s", rentNewCar.renterName);
printf("Enter number of days rental needed: ");
scanf("%d", &rentNewCar.daysRenting);
printf("Enter Car ID: );
scanf("%d", &rentNewCar.carId);
if(size < 60) {
allRentals[size + i] = rentNewCar;
i++;
totalRentals --;
}}}
int findCarIDByMake(Car *allCars, int totalCars, char *carMake) {
// get Car ID by Make
int i;
for (i=0; i < totalCars; i++) {
if (strcmp(allCars[i].make, carMake) == 0)
return allCars[i].carId;
}
}
int findReservation(Rental *allRentals, int totalRentals, char *renterName){
do {
printf("Enter Renter Name: ");
scanf("%s", renterName);
reservedCar = findReservation(allRentals, totalRentals, renterName);
while {
(reservedCar ==-1);
printf("Renter Name: %s", allRentals[reservedCar].renterName);
printf(" Days Renting: %s", allRentals[reservedCar].daysRenting);
printCarInfo(allCars, totalCars, allRentals[reservedCar].daysRenting);
}
}
int findCarbyId(Car *allCars, int totalCars, int carId) {
// get id and return Car
int i;
for(i=0; i < totalCars; i++){
if (allCars[i].carId == carId) {
return i;
}
}
}
void printCarInfo(Car *allCars, int totalCars, int carId) {
int i;
for (i=0; i< totalCars; i++) {
if (allCars[i].carId == carId) {
printf("Make: %s", allCars[i].make);
printf(" Model: %s", allCars[i].model);
printf(" Model: %s", allCars[i].model);
}
}
}
void printAllRentals(Rental *allRentals, int totalRentals, Car *allCars, int totalCars){
int i;
for (i = 0; i< totalRentals; i++) {
printf("Renter's Name: %s", allRentals[i].renterName);
printCarInfo(allCars, totalCars, allRentals[i].carId);
}
}
double getAverageRentalDays(Rental *allRentals, int totalRentals) {
int i;
double avg;
double sum;
while (totalRentals !=0) {
for(i =0; i < totalRentals; i++) {
sum += allRentals[i].daysRenting; }
avg = sum/ totalRentals;
}
}
int main(void) {
char allCars[20];
Rental allRentals[60];
int totalRentals;
int menuChoice;
int totalCars =0;
totalCars = createInventory(allCars);
while(menuChoice != '7')
{
printf("Menu ");
printf(" 1) Add new car to the inventory ");
printf(" 2) Make a reservation ");
printf(" 3) Find a reservation ");
printf(" 4) Print rented and non-rented vehicles ");
printf(" 5) Compute average number of days rented ");
scanf("%d", &menuChoice);
switch(menuChoice) {
case 1:
totalCars = addNewCar(allCars, totalCars, size);
break;
case 2:
totalRentals = addNewRental(allCars, totalCars, allRentals, totalRentals
break;
case 3:
findReservation(totalCars, allRentals, totalRentals);
break;
case 4:
printAllRentals(allRentals, totalRentals, allCars, totalCars);
break;
case 5:
printf("Enter make of Car: ");
scanf("%s", carMake);
carId = findCarIDByMake(allCars, totalCars, carMake);
printCarInfo(allCars, totalCars, carId);
break;
case 6:
average = getAverageRentalDays(allRentals, totalRentals);
printf("Average Rental Days: %lf", average);
break;
}
}
}
Explanation / Answer
I removed all the compilation errors that you asked. However there are some logical errors that needs to be corrected to effectively make it run and generate right outputs.
#include <stdio.h>
#include <string.h>
typedef struct Car {
int carId;
char make[20];
char model[20];
int numDoors;
double rate;
}Car;
typedef struct Rental{
char renterName[20];
int daysRenting;
int carId;
}Rental;
int createInventory(Car *allCars) {
//car1
allCars[0].carId = 1234;
strcpy(allCars[0].make, "vw");
strcpy(allCars[0].model, "Golf");
allCars[0].numDoors= 2;
allCars[0].rate = 66;
//car2
allCars[1].carId = 2241;
strcpy(allCars[1].make, "Ford");
strcpy(allCars[1].model, "Focus");
allCars[1].numDoors= 4;
allCars[1].rate = 45;
//car3
allCars[2].carId = 3445;
strcpy(allCars[2].make, "bmw");
strcpy(allCars[2].model, "x3");
allCars[2].numDoors= 4;
allCars[2].rate = 128;
return 3;
}
int addNewCar(Car *allCars, int totalCars, int size) {
printf("Enter number of cars to add: ");
scanf("%d", &totalCars);
int i=1;
while(i <= totalCars)
{
if(i == 20) {
printf("Max number of cars reached");
break;
}
Car newCar;
printf("Enter car ID: ");
scanf("%d", &newCar.carId);
printf("Enter car make: ");
scanf("%s", newCar.make);
printf("Enter car model: ");
scanf("%s", newCar.model);
printf("Enter number of car doors: ");
scanf("%d", &newCar.numDoors);
printf("Enter car rating: ");
scanf("%lf", &newCar.rate);
if(size < 20)
allCars[size + 1] = newCar;
size = size + 1;
i = i + 1;
return totalCars;
}
}
int addNewRental(Car *allCars, int totalCars, Rental *allRentals, int totalRentals, int size) {
int i=0;
while (totalRentals <= 60) {
Rental rentNewCar;
printf("Enter renter name: ");
scanf("%s", rentNewCar.renterName);
printf("Enter number of days rental needed: ");
scanf("%d", &rentNewCar.daysRenting);
printf("Enter Car ID: ");
scanf("%d", &rentNewCar.carId);
if(size < 60) {
allRentals[size + i] = rentNewCar;
i++;
totalRentals --;
}}
}
int findCarIDByMake(Car *allCars, int totalCars, char *carMake) {
// get Car ID by Make
int i;
for (i=0; i < totalCars; i++) {
if (strcmp(allCars[i].make, carMake) == 0)
return allCars[i].carId;
}
}
void printCarInfo(Car *allCars, int totalCars, int carId) {
int i;
for (i=0; i< totalCars; i++) {
if (allCars[i].carId == carId) {
printf("Make: %s", allCars[i].make);
printf(" Model: %s", allCars[i].model);
printf(" Model: %s", allCars[i].model);
}
}
}
int findReservation(Rental *allRentals, int totalRentals, Car *allCars, int totalCars){
char renterName[20];
printf("Enter Renter Name: ");
scanf("%s", renterName);
int k;
for( k =0;k< sizeof(allRentals);k++){
if(allRentals[k].renterName==renterName){
int reservedCar =k ;
printf("Renter Name: %s", allRentals[reservedCar].renterName);
printf(" Days Renting: %d", allRentals[reservedCar].daysRenting);
printCarInfo(allCars, totalCars, allRentals[reservedCar].daysRenting);
}
}
}
int findCarbyId(Car *allCars, int totalCars, int carId) {
// get id and return Car
int i;
for(i=0; i < totalCars; i++){
if (allCars[i].carId == carId) {
return i;
}
}
}
void printAllRentals(Rental *allRentals, int totalRentals, Car *allCars, int totalCars){
int i;
for (i = 0; i< totalRentals; i++) {
printf("Renter's Name: %s", allRentals[i].renterName);
printCarInfo(allCars, totalCars, allRentals[i].carId);
}
}
double getAverageRentalDays(Rental *allRentals, int totalRentals) {
int i;
double avg;
double sum;
while (totalRentals !=0) {
for(i =0; i < totalRentals; i++) {
sum += allRentals[i].daysRenting; }
avg = sum/ totalRentals;
}
}
int main(void) {
struct Car allCars[20];
struct Rental allRentals[60];
int totalRentals = 0;
int menuChoice;
int totalCars =0;
char carMake[20];
int size = 0;
totalCars = createInventory(allCars);
while(menuChoice != '7')
{
printf("Menu ");
printf(" 1) Add new car to the inventory ");
printf(" 2) Make a reservation ");
printf(" 3) Find a reservation ");
printf(" 4) Print rented and non-rented vehicles ");
printf(" 5) Compute average number of days rented ");
scanf("%d", &menuChoice);
switch(menuChoice) {
case 1:
totalCars = addNewCar(allCars, totalCars, size);
break;
case 2:
totalRentals = addNewRental(allCars, totalCars, allRentals, totalRentals, size);
break;
case 3:
findReservation( allRentals, totalRentals, allCars, totalCars);
break;
case 4:
printAllRentals(allRentals, totalRentals, allCars, totalCars);
break;
case 5:
printf("Enter make of Car: ");
scanf("%s", carMake);
int carId = findCarIDByMake(allCars, totalCars, carMake);
printCarInfo(allCars, totalCars, carId);
break;
case 6:
printf("Average Rental Days: ");
double average = getAverageRentalDays(allRentals, totalRentals);
printf("%lf", average);
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.