Am i implementing the writeReserv and the readReserv correctly, and also im also
ID: 3574723 • Letter: A
Question
Am i implementing the writeReserv and the readReserv correctly, and also im also getting a segmentation fault when i try to add a car to the inventory?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define true 1
typedef short int BoolT;
typedef struct {
int carId; // a unique ID of a car
char make[20]; // make of the car
char model[20]; // model of the car
int numDoors; // 2 or 4 door cars
double rate; // the rental rate of the car
BoolT use; // 0 indicates car is not in use, 1 indicates car is in use
} CarT;
typedef struct {
char renterName[20]; // name of the renter
int daysRenting; // days renting the car
int carId; // ID of the car rented
} RentalT;
typedef struct {
char renterName[20]; // name of the renter
int daysRenting; // days renting the car
char make[20]; // the make of a reserved car
char model[20]; // the model of a reserved car
} ReservT;
typedef struct {
CarT allCars[20]; // a list of cars
RentalT allRentals[20]; // a list of rentals
ReservT allReserv[20]; // a list of reservations
int totalCars; // number of all cars owned
int totalRentals; // number of rented cars
int totalReserv; // number of reservations
} CompanyT;
/**
* Prints a menu to the screen.
*/
void printMenu();
/**
* Handles a menu selection.
*
* @param choice - the menu choice the user made
* @param company - the rental company
*/
void handleSelection ( int choice, CompanyT *company );
/**
* Create a new inventory.
*
* @param company - records of a company
*/
void addNewCar ( CompanyT *company );
/**
* Creates a new reservation for a car rental.
*
* @param company - records of a company
*/
void makeReservation ( CompanyT *company );
/**
* Find a car reservation among all reservations.
* The function prompts the user for a name to look-up a reservation record.
*
* @param company - records of a company
*/
void findReservation ( CompanyT *company );
/**
* Check-out a car for a given reservation.
* The function prompts the user for a reservation name, looks-up the
* reserved car and checks it out. A new rental record is created and
* the reservation record is removed. If there is no free car available
* of the reserved car a list of available cars is printed for the user
* to choose a car and check it out.
*
* @param company - records of a company
*/
void checkOut ( CompanyT *company );
/**
* Removes a reservation from the records of reservations given a customer name.
*
* @param company - records of a company
* @param renterName - the name of a customer who made a reservation
*
*/
void removeReserv (CompanyT *company, char *renterName);
/**
* Prints a list of cars to the screen that are not in use.
*
* @param company - records of a company
*
*/
void printAllFreeCars ( CompanyT *company );
/**
* Computes average days of car rentals.
*
* @param company - records of a company
*
* @return the average rental days or -1 if no car has been rented
*/
double getAverageRentalDays ( CompanyT *company );
//writes the rental information to a text file specified in command line
void writeRentals ( char *filename, RentalT *allRentals, int totalRentals );
//reads the rental information from the text file mentioned in command line
int readRentals (char *filename, RentalT *allRentals );
//writes the car information to a text file specified in command line
void writeCars ( char *filename, CarT *allCars, int totalCars );
//reads the car information from the text file mentioned in command line
int readCars (char *filename, CarT *allCars );
//writes the reserve information to a text file specified in command line
void writeReserv (char *filename, ReservT *allReserv, int totalReserv);
//reads the reserve information to a text file specified in command line
int main ( int argc, char *argv[] )
{
CompanyT myRentalCompany;
CarT allCars[20];
RentalT allRentals[60];
ReservT allReserv[60];
int choice;
int totalCars;
int totalRentals;
int totalReserv;
// add pre-defined list of cars to the list
totalCars=readCars(argv[1], allCars);
totalRentals=readRentals(argv[2], allRentals);
totalReserv=readReserv(argv[3], allReserv);
// there are no reservations and no rentals at this point
myRentalCompany.totalRentals = 0;
myRentalCompany.totalReserv = 0;
while ( true ) {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
printf (" ");
if (choice == 7)
break;
handleSelection (choice, &myRentalCompany );
};
writeRentals(argv[2], allRentals, totalRentals);
writeCars(argv[1], allCars, totalCars);
writeReserv(argv[3], allReserv, totalReserv);
return 0;
}
void printMenu()
{
printf ("1 Add new car to the inventory. ");
printf ("2 Make a reservation. ");
printf ("3 Find a reservation using a renter name and print it to the screen. ");
printf ("4 Check out a car given a renter name. ");
printf ("5 Print all available cars. ");
printf ("6 Calculate and print average number of days rented. ");
printf ("7 Exit program. ");
}
void handleSelection ( int choice, CompanyT *company )
{
double value;
switch ( choice ) {
case 1 : addNewCar ( company ); break;
case 2 : makeReservation ( company ); break;
case 3 : findReservation ( company ); break;
case 4 : checkOut ( company ); break;
case 5 : printAllFreeCars ( company ); break;
case 6 : value = getAverageRentalDays ( company );
if (value >= 0) {
printf ("Average number of days cars are rented is %3.2lf. ", value );
} else {
printf ("No cars have been rented. ");
}
break;
default : printf ("Invalid entry.");
}
printf (" ");
}
void addNewCar ( CompanyT *company )
{
if (company->totalCars == 20)
return ;
int index = company->totalCars;
printf ("Enter car ID: ");
scanf ("%d", &company->allCars[index].carId);
printf ("Enter make: ");
scanf ("%s", company->allCars[index].make);
printf ("Enter model: ");
scanf ("%s", company->allCars[index].model);
printf ("Enter number of doors (2 or 4): ");
scanf ("%d", &company->allCars[index].numDoors);
printf ("Enter car rate: ");
scanf ("%lf", &company->allCars[index].rate);
company->allCars[index].use = 0;
company->totalCars++;
}
void makeReservation ( CompanyT *company )
{
int index = company->totalReserv;
printf ("Enter renter name: ");
scanf ("%s", company->allReserv[index].renterName);
printf ("Enter number of days of rental: ");
scanf ("%d", &company->allReserv[index].daysRenting);
printf ("Enter the make of car you want to rent: ");
scanf ("%s", company->allReserv[index].make);
printf ("Enter the model of the car you want to rent: ");
scanf ("%s", company->allReserv[index].model);
company->totalReserv++;
}
void findReservation ( CompanyT *company )
{
int i, index;
char name[20];
printf ("Enter reservation name: ");
scanf ("%s", name);
// assume reservation doesn't exist
index = -1;
for (i=0; i < company->totalReserv; i++) {
if (!strcmp(company->allReserv[i].renterName, name)) {
index = i;
break;
}
}
if (index >= 0) {
printf ("Renter: %s ", company->allReserv[index].renterName);
printf ("Days renting: %d ", company->allReserv[index].daysRenting);
printf ("Make of car: %s ", company->allReserv[index].make);
printf ("Model of car: %s ", company->allReserv[index].model);
} else {
printf ("There is no reservation under the name %s. ", name);
}
}
/**
* Checks out a car that matches the make and model by marking it as used.
*
* @param company - records of a company
* @param make - the make of a car to be checked out
* @param model - the model of a car to be checked out
*
* @return the car Id of the matching car or -1 if there is no free car that
* matches the make and model
*/
int checkOutCar (CompanyT *company, char *make, char *model)
{
int i;
for (i=0; i < company->totalCars; i++) {
if (!strcmp(company->allCars[i].make, make) &&
!strcmp(company->allCars[i].model, model) &&
company->allCars[i].use == 0) {
company->allCars[i].use = 1;
return company->allCars[i].carId;
}
}
// matching car not found
return -1;
}
/**
* Sets a car with a given ID in use.
*
* @param company - records of a company
* @param carId - the Id of the car to be set in use
*
* @return 1 if the car was selected to be in use, 0 if an error occurred
*/
int setCarInUse (CompanyT *company, int carId)
{
int i;
for (i=0; i < company->totalCars; i++) {
if (company->allCars[i].carId == carId) {
company->allCars[i].use = 1;
return 1;
}
}
// car Id did not match any car
return 0;
}
void checkOut ( CompanyT *company )
{
int i, indexReserv;
char name[20];
printf ("Enter name on the reservation: ");
scanf ("%s", name);
// search for reservations
indexReserv = -1;
for (i=0; i < company->totalReserv; i++) {
if (!strcmp(company->allReserv[i].renterName, name)) {
indexReserv = i;
break;
}
}
if (indexReserv == -1) {
printf ("A reservation under the name %s does not exist. First create a reservation. ", name);
return; // nothing to do
}
// find available car matching requested make and model and marks it as
// checked-out
int carId = checkOutCar (company,
company->allReserv[indexReserv].make,
company->allReserv[indexReserv].model);
if (carId == -1) {
// no car found; print available car
printf ("We do not have the reserved car available. Would you like to rent one of the these? ");
printAllFreeCars ( company );
printf ("Enter car ID or -1 if reservation should be cancled and no car should be selected: ");
scanf ("%d", &carId);
if (carId == -1) {
removeReserv (company, name);
return;
}
if (!setCarInUse(company, carId)) {
printf ("Invalid carId. We cannot checkout the selected car. ");
}
}
// construct new car record
int indexRentals = company->totalRentals;
strcpy (company->allRentals[indexRentals].renterName, name);
company->allRentals[indexRentals].daysRenting = company->allReserv[indexReserv].daysRenting;
company->allRentals[indexRentals].carId = carId;
// increment total rentals
company->totalRentals++;
// remove reservation; car has been succesfully checked out
removeReserv (company, name);
printf ("The car has been checked out. ");
}
void removeRecord (ReservT *allReserv, int index, int totalReserv)
{
int i;
for (i=index; i < totalReserv-1; i++) {
// company records from position i+1 to position identified
allReserv[i] = allReserv[i+1];
}
}
void removeReserv (CompanyT *company, char *renterName)
{
// locate reservation record matching the given name
int i;
for (i=0; i < company->totalReserv; i++) {
if (!strcmp(company->allReserv[i].renterName, renterName)) {
// identified matching record
// remove record by shifting records downwards
removeRecord (company->allReserv, i, company->totalReserv);
// reduce the total number of reservation records by one
company->totalReserv--;
}
}
}
void printAllFreeCars ( CompanyT *company )
{
int i;
int unusedCars = 0;
printf ("CarId Make Model Num Doors Rate ");
printf ("-------------------------------------------------------------- ");
for (i=0; i < company->totalCars; i++ ) {
if ( company->allCars[i].use == 0 ) {
printf ("%d ", company->allCars[i].carId);
printf ("%s ", company->allCars[i].make);
printf ("%s ", company->allCars[i].model);
printf ("%d ", company->allCars[i].numDoors);
printf ("%3.2lf ", company->allCars[i].rate);
unusedCars = 1;
}
}
if (!unusedCars) {
printf ("There are no cars available. ");
}
}
double getAverageRentalDays ( CompanyT *company )
{
int i;
double sum = 0.0;
if (company->totalRentals == 0) {
// no rental records exist
return -1;
}
for (i=0; i < company->totalRentals; i++ ) {
sum += company->allRentals[i].daysRenting;
}
return sum / company->totalRentals;
}
void writeRentals ( char *filename, RentalT *allRentals, int totalRentals )
{
int i;
FILE *out_file = fopen(filename, "w");
for(i=0; i<totalRentals; i++)
{
fprintf(out_file, "%s ", allRentals[i].renterName);
fprintf(out_file, "%d ", allRentals[i].daysRenting);
fprintf(out_file, "%d ", allRentals[i].carId);
}
fclose(out_file);
}
int readRentals (char *filename, RentalT *allRentals )
{
FILE *in_file = fopen(filename, "r");
int counter = 0;
while(EOF !=fscanf(in_file, "%s %d %d", &allRentals[counter].renterName , &allRentals[counter].daysRenting, &allRentals[counter].carId))
{
counter++;
}
return counter;
}
void writeCars ( char *filename, CarT *allCars, int totalCars )
{
int i;
FILE *out_file = fopen(filename, "w");
for(i=0; i<totalCars; i++)
{
fprintf(out_file, "%d ", allCars[i].carId);
fprintf(out_file, "%s ", allCars[i].make);
fprintf(out_file, "%s ", allCars[i].model);
fprintf(out_file, "%d ", allCars[i].numDoors);
fprintf(out_file, "%f ", allCars[i].rate);
}
}
int readCars (char *filename, CarT *allCars )
{
FILE *in_file = fopen(filename, "r");
int counter = 0;
while(EOF !=fscanf(in_file, "%d %s %s %d %f", &allCars[counter].carId , &allCars[counter].make, &allCars[counter].model, &allCars[counter].numDoors, &allCars[counter].rate))
{
counter++;
}
return counter;
}
void writeReserv (char *filename, ReservT *allReserv, int totalReserv )
{
int i;
FILE *out_file = fopen(filename, "w");
for(i=0; i<totalReserv; i++)
{
fprintf(out_file, "%s", allReserv[i].renterName);
fprintf(out_file, "%d", allReserv[i].daysRenting);
fprintf(out_file, "%s", allReserv[i].make);
fprintf(out_file, "%S", allReserv[i].model);
}
fclose(out_file);
}
int readReserv (char *filename, ReservT *allReserv)
{
FILE *in_file = fopen(filename, "r");
int counter = 0;
while(EOF !=fscanf(in_file, "%s %d %s %s", &allReserv[counter].renterName , &allReserv[counter].daysRenting , &allReserv[counter].make , &allReserv[counter].model))
{
counter++;
}
return counter;
}
Explanation / Answer
Made a couple of changes in the code. Add cars is working fine now working fine now
PROGRAM CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define true 1
typedef short int BoolT;
typedef struct {
int carId; // a unique ID of a car
char make[20]; // make of the car
char model[20]; // model of the car
int numDoors; // 2 or 4 door cars
double rate; // the rental rate of the car
BoolT use; // 0 indicates car is not in use, 1 indicates car is in use
} CarT;
typedef struct {
char renterName[20]; // name of the renter
int daysRenting; // days renting the car
int carId; // ID of the car rented
} RentalT;
typedef struct {
char renterName[20]; // name of the renter
int daysRenting; // days renting the car
char make[20]; // the make of a reserved car
char model[20]; // the model of a reserved car
} ReservT;
typedef struct {
CarT allCars[20]; // a list of cars
RentalT allRentals[20]; // a list of rentals
ReservT allReserv[20]; // a list of reservations
int totalCars; // number of all cars owned
int totalRentals; // number of rented cars
int totalReserv; // number of reservations
} CompanyT;
/**
* Prints a menu to the screen.
*/
void printMenu();
/**
* Handles a menu selection.
*
* @param choice - the menu choice the user made
* @param company - the rental company
*/
void handleSelection ( int choice, CompanyT *company );
/**
* Create a new inventory.
*
* @param company - records of a company
*/
void addNewCar ( CompanyT *company );
/**
* Creates a new reservation for a car rental.
*
* @param company - records of a company
*/
void makeReservation ( CompanyT *company );
/**
* Find a car reservation among all reservations.
* The function prompts the user for a name to look-up a reservation record.
*
* @param company - records of a company
*/
void findReservation ( CompanyT *company );
/**
* Check-out a car for a given reservation.
* The function prompts the user for a reservation name, looks-up the
* reserved car and checks it out. A new rental record is created and
* the reservation record is removed. If there is no free car available
* of the reserved car a list of available cars is printed for the user
* to choose a car and check it out.
*
* @param company - records of a company
*/
void checkOut ( CompanyT *company );
/**
* Removes a reservation from the records of reservations given a customer name.
*
* @param company - records of a company
* @param renterName - the name of a customer who made a reservation
*
*/
void removeReserv (CompanyT *company, char *renterName);
/**
* Prints a list of cars to the screen that are not in use.
*
* @param company - records of a company
*
*/
void printAllFreeCars ( CompanyT *company );
/**
* Computes average days of car rentals.
*
* @param company - records of a company
*
* @return the average rental days or -1 if no car has been rented
*/
double getAverageRentalDays ( CompanyT *company );
//writes the rental information to a text file specified in command line
void writeRentals ( char *filename, RentalT *allRentals, int totalRentals );
//reads the rental information from the text file mentioned in command line
int readRentals (char *filename, RentalT *allRentals );
//writes the car information to a text file specified in command line
void writeCars ( char *filename, CarT *allCars, int totalCars );
//reads the car information from the text file mentioned in command line
int readCars (char *filename, CarT *allCars );
//writes the reserve information to a text file specified in command line
void writeReserv (char *filename, ReservT *allReserv, int totalReserv);
//reads the reserve information to a text file specified in command line
int readReserv (char *filename, ReservT *allReserv);
int main ( int argc, char *argv[] )
{
CompanyT myRentalCompany;
CarT allCars[20];
RentalT allRentals[60];
ReservT allReserv[60];
int choice;
int totalCars;
int totalRentals;
int totalReserv;
// add pre-defined list of cars to the list
totalCars=readCars(argv[1], allCars);
totalRentals=readRentals(argv[2], allRentals);
totalReserv=readReserv(argv[3], allReserv);
// there are no reservations and no rentals at this point
myRentalCompany.totalRentals = 0;
myRentalCompany.totalReserv = 0;
//assigning the total cars read ffrom file to company cars count
myRentalCompany.totalCars = totalCars;
while ( true ) {
printMenu();
printf ("Choose option: ");
scanf ("%d", &choice);
printf (" ");
if (choice == 7)
break;
handleSelection (choice, &myRentalCompany );
};
writeRentals(argv[2], allRentals, totalRentals);
writeCars(argv[1], allCars, totalCars);
writeReserv(argv[3], allReserv, totalReserv);
return 0;
}
void printMenu()
{
printf ("1 Add new car to the inventory. ");
printf ("2 Make a reservation. ");
printf ("3 Find a reservation using a renter name and print it to the screen. ");
printf ("4 Check out a car given a renter name. ");
printf ("5 Print all available cars. ");
printf ("6 Calculate and print average number of days rented. ");
printf ("7 Exit program. ");
}
void handleSelection ( int choice, CompanyT *company )
{
double value;
switch ( choice ) {
case 1 : addNewCar ( company ); break;
case 2 : makeReservation ( company ); break;
case 3 : findReservation ( company ); break;
case 4 : checkOut ( company ); break;
case 5 : printAllFreeCars ( company ); break;
case 6 : value = getAverageRentalDays ( company );
if (value >= 0) {
printf ("Average number of days cars are rented is %3.2lf. ", value );
} else {
printf ("No cars have been rented. ");
}
break;
default : printf ("Invalid entry.");
}
printf (" ");
}
void addNewCar ( CompanyT *company )
{
if (company->totalCars == 20)
return ;
int index = company->totalCars;
printf ("Enter car ID: ");
scanf ("%d", &company->allCars[index].carId);
printf ("Enter make: ");
scanf ("%s", company->allCars[index].make);
printf ("Enter model: ");
scanf ("%s", company->allCars[index].model);
printf ("Enter number of doors (2 or 4): ");
scanf ("%d", &company->allCars[index].numDoors);
printf ("Enter car rate: ");
scanf ("%lf", &company->allCars[index].rate);
company->allCars[index].use = 0;
company->totalCars++;
}
void makeReservation ( CompanyT *company )
{
int index = company->totalReserv;
printf ("Enter renter name: ");
scanf ("%s", company->allReserv[index].renterName);
printf ("Enter number of days of rental: ");
scanf ("%d", &company->allReserv[index].daysRenting);
printf ("Enter the make of car you want to rent: ");
scanf ("%s", company->allReserv[index].make);
printf ("Enter the model of the car you want to rent: ");
scanf ("%s", company->allReserv[index].model);
company->totalReserv++;
}
void findReservation ( CompanyT *company )
{
int i, index;
char name[20];
printf ("Enter reservation name: ");
scanf ("%s", name);
// assume reservation doesn't exist
index = -1;
for (i=0; i < company->totalReserv; i++) {
if (!strcmp(company->allReserv[i].renterName, name)) {
index = i;
break;
}
}
if (index >= 0) {
printf ("Renter: %s ", company->allReserv[index].renterName);
printf ("Days renting: %d ", company->allReserv[index].daysRenting);
printf ("Make of car: %s ", company->allReserv[index].make);
printf ("Model of car: %s ", company->allReserv[index].model);
} else {
printf ("There is no reservation under the name %s. ", name);
}
}
/**
* Checks out a car that matches the make and model by marking it as used.
*
* @param company - records of a company
* @param make - the make of a car to be checked out
* @param model - the model of a car to be checked out
*
* @return the car Id of the matching car or -1 if there is no free car that
* matches the make and model
*/
int checkOutCar (CompanyT *company, char *make, char *model)
{
int i;
for (i=0; i < company->totalCars; i++) {
if (!strcmp(company->allCars[i].make, make) &&
!strcmp(company->allCars[i].model, model) &&
company->allCars[i].use == 0) {
company->allCars[i].use = 1;
return company->allCars[i].carId;
}
}
// matching car not found
return -1;
}
/**
* Sets a car with a given ID in use.
*
* @param company - records of a company
* @param carId - the Id of the car to be set in use
*
* @return 1 if the car was selected to be in use, 0 if an error occurred
*/
int setCarInUse (CompanyT *company, int carId)
{
int i;
for (i=0; i < company->totalCars; i++) {
if (company->allCars[i].carId == carId) {
company->allCars[i].use = 1;
return 1;
}
}
// car Id did not match any car
return 0;
}
void checkOut ( CompanyT *company )
{
int i, indexReserv;
char name[20];
printf ("Enter name on the reservation: ");
scanf ("%s", name);
// search for reservations
indexReserv = -1;
for (i=0; i < company->totalReserv; i++) {
if (!strcmp(company->allReserv[i].renterName, name)) {
indexReserv = i;
break;
}
}
if (indexReserv == -1) {
printf ("A reservation under the name %s does not exist. First create a reservation. ", name);
return; // nothing to do
}
// find available car matching requested make and model and marks it as
// checked-out
int carId = checkOutCar (company,
company->allReserv[indexReserv].make,
company->allReserv[indexReserv].model);
if (carId == -1) {
// no car found; print available car
printf ("We do not have the reserved car available. Would you like to rent one of the these? ");
printAllFreeCars ( company );
printf ("Enter car ID or -1 if reservation should be cancled and no car should be selected: ");
scanf ("%d", &carId);
if (carId == -1) {
removeReserv (company, name);
return;
}
if (!setCarInUse(company, carId)) {
printf ("Invalid carId. We cannot checkout the selected car. ");
}
}
// construct new car record
int indexRentals = company->totalRentals;
strcpy (company->allRentals[indexRentals].renterName, name);
company->allRentals[indexRentals].daysRenting = company->allReserv[indexReserv].daysRenting;
company->allRentals[indexRentals].carId = carId;
// increment total rentals
company->totalRentals++;
// remove reservation; car has been succesfully checked out
removeReserv (company, name);
printf ("The car has been checked out. ");
}
void removeRecord (ReservT *allReserv, int index, int totalReserv)
{
int i;
for (i=index; i < totalReserv-1; i++) {
// company records from position i+1 to position identified
allReserv[i] = allReserv[i+1];
}
}
void removeReserv (CompanyT *company, char *renterName)
{
// locate reservation record matching the given name
int i;
for (i=0; i < company->totalReserv; i++) {
if (!strcmp(company->allReserv[i].renterName, renterName)) {
// identified matching record
// remove record by shifting records downwards
removeRecord (company->allReserv, i, company->totalReserv);
// reduce the total number of reservation records by one
company->totalReserv--;
}
}
}
void printAllFreeCars ( CompanyT *company )
{
int i;
int unusedCars = 0;
printf ("CarId Make Model Num Doors Rate ");
printf ("-------------------------------------------------------------- ");
for (i=0; i < company->totalCars; i++ ) {
if ( company->allCars[i].use == 0 ) {
printf ("%d ", company->allCars[i].carId);
printf ("%s ", company->allCars[i].make);
printf ("%s ", company->allCars[i].model);
printf ("%d ", company->allCars[i].numDoors);
printf ("%3.2lf ", company->allCars[i].rate);
unusedCars = 1;
}
}
if (!unusedCars) {
printf ("There are no cars available. ");
}
}
double getAverageRentalDays ( CompanyT *company )
{
int i;
double sum = 0.0;
if (company->totalRentals == 0) {
// no rental records exist
return -1;
}
for (i=0; i < company->totalRentals; i++ ) {
sum += company->allRentals[i].daysRenting;
}
return sum / company->totalRentals;
}
void writeRentals ( char *filename, RentalT *allRentals, int totalRentals )
{
int i;
FILE *out_file = fopen(filename, "w");
for(i=0; i<totalRentals; i++)
{
fprintf(out_file, "%s ", allRentals[i].renterName);
fprintf(out_file, "%d ", allRentals[i].daysRenting);
fprintf(out_file, "%d ", allRentals[i].carId);
}
fclose(out_file);
}
int readRentals (char *filename, RentalT *allRentals )
{
FILE *in_file = fopen(filename, "r");
int counter = 0;
while(EOF !=fscanf(in_file, "%s %d %d", &allRentals[counter].renterName , &allRentals[counter].daysRenting, &allRentals[counter].carId))
{
counter++;
}
return counter;
}
void writeCars ( char *filename, CarT *allCars, int totalCars )
{
int i;
FILE *out_file = fopen(filename, "w");
for(i=0; i<totalCars; i++)
{
fprintf(out_file, "%d ", allCars[i].carId);
fprintf(out_file, "%s ", allCars[i].make);
fprintf(out_file, "%s ", allCars[i].model);
fprintf(out_file, "%d ", allCars[i].numDoors);
fprintf(out_file, "%f ", allCars[i].rate);
}
}
int readCars (char *filename, CarT *allCars )
{
FILE *in_file = fopen(filename, "r");
int counter = 0;
while(EOF !=fscanf(in_file, "%d %s %s %d %f ", &allCars[counter].carId , &allCars[counter].make, &allCars[counter].model, &allCars[counter].numDoors, &allCars[counter].rate))
{
counter++;
}
return counter;
}
void writeReserv (char *filename, ReservT *allReserv, int totalReserv )
{
int i;
FILE *out_file = fopen(filename, "w");
for(i=0; i<totalReserv; i++)
{
fprintf(out_file, "%s", allReserv[i].renterName);
fprintf(out_file, "%d", allReserv[i].daysRenting);
fprintf(out_file, "%s", allReserv[i].make);
fprintf(out_file, "%s", allReserv[i].model);
}
fclose(out_file);
}
int readReserv (char *filename, ReservT *allReserv)
{
FILE *in_file = fopen(filename, "r");
int counter = 0;
while(EOF !=fscanf(in_file, "%s %d %s %s", &allReserv[counter].renterName , &allReserv[counter].daysRenting , &allReserv[counter].make , &allReserv[counter].model))
{
counter++;
}
return counter;
}
OUTPUT:
1 Add new car to the inventory.
2 Make a reservation.
3 Find a reservation using a renter name and print it to the screen.
4 Check out a car given a renter name.
5 Print all available cars.
6 Calculate and print average number of days rented.
7 Exit program.
Choose option: 1
Enter car ID: 7658
Enter make: Hyundai
Enter model: GWE345
Enter number of doors (2 or 4): 4
Enter car rate: 842.56
1 Add new car to the inventory.
2 Make a reservation.
3 Find a reservation using a renter name and print it to the screen.
4 Check out a car given a renter name.
5 Print all available cars.
6 Calculate and print average number of days rented.
7 Exit program.
Choose option: 5
CarId Make Model Num Doors Rate
--------------------------------------------------------------
7658 Hyundai GWE345 4 842.56
1 Add new car to the inventory.
2 Make a reservation.
3 Find a reservation using a renter name and print it to the screen.
4 Check out a car given a renter name.
5 Print all available cars.
6 Calculate and print average number of days rented.
7 Exit program.
Choose option: 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.