Answered Essay: Objectives: The two main objectives of this project is to test your ability to (1) create and use structs with arra

Code must be in C++. Thank you for your time!

Objectives: The two main objectives of this project is to test your ability to (1) create and use structs with arrays, (2) work with pointers, pointer arithmetic, pass by-Value, pass by-Reference, pass by-Address, and (3) design, implement and test a solution to a given problem. A review of your knowledge of arrays, iostream, file I/O and C-style strings is also included. Description: For this project, you are to create a program that will assist users who want to rent a car. You are given a datafile with 5 different cars file (the file is a priori known to have exactly 5 entries, each following the same data lavout), and you must read in all of the car data from the file and store it in an array of structs. You must also create a menu with the functionality defined below Although an example file is provided (Cars.txt), for grading purposes your project will be tested against a different test file that will not be provided to you beforehand. Our test file will be in the same format as the example file. The RentalCar struct will contain the following data members: year, an int (year of production) make, a C-string (char array of 10 maximum size) model, a C-string (char array of 10 maximum size) price, a float (price per day) available, a bool (1true, 0 - false; try to display true/false using the std: boolalpha manipulator like: cout << boolalpha << boolVariable;) The menu must have the following entries, each implementing a functionality: 1) Ask the user for the input file name, and then read ALL data from that file. The data have to be stored into an array of structs 2) Print out ALL data for all of the cars to the terminal 3) Print out ALL data for all of the cars to a separate output file (when the user chooses menu entry 2, they should also get asked for an output file name) 4) Sort the cars (i.e. sort the array of structs) by ascending price. 5) Ask the user for how many days they want to rent a car. Then print to the terminal only the available cars, sorted by ascending price, as well as the total estimated cost to make the rent (number of days multiplied by price per day) 6) Ask the user which car they want to rent (expected user input is an index number for the array of structs) and for how many days. If the corresponding car is not available, print a warning message to terminal. If it is available, mark it as rented (modify the available member appropriately) and print out to terminal a success message that mentions the total cost (number of days multiplied by price per day) 7 Exit program

Objectives: The two main objectives of this project is to test your ability to (1) create and use structs with arrays, (2) work with pointers, pointer arithmetic, pass by-Value, pass by-Reference, pass by-Address, and (3) design, implement and test a solution to a given problem. A review of your knowledge of arrays, iostream, file I/O and C-style strings is also included. Description: For this project, you are to create a program that will assist users who want to rent a car. You are given a datafile with 5 different cars file (the file is a priori known to have exactly 5 entries, each following the same data lavout), and you must read in all of the car data from the file and store it in an array of structs. You must also create a menu with the functionality defined below Although an example file is provided (Cars.txt), for grading purposes your project will be tested against a different test file that will not be provided to you beforehand. Our test file will be in the same format as the example file. The RentalCar struct will contain the following data members: year, an int (year of production) make, a C-string (char array of 10 maximum size) model, a C-string (char array of 10 maximum size) price, a float (price per day) available, a bool (1true, 0 – false; try to display true/false using the “std: boolalpha” manipulator like: cout

Expert Answer

 

// C++ code

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
const int CAR_COUNT = 10;

struct rentalCar {
char make[20];
char model[20];
int year;
float price;
bool available;
};

void select(int &selection);
void readFile(rentalCar cars[]);
void printCars(bool availableOnly, rentalCar cars[]);
void estimatePrice(rentalCar cars[]);
void mostExpensive(rentalCar cars[]);

main() {
/* Display Menu */
int selection = 0; // holds selection number
do {
/* Print Menu Title */
cout << setfill(‘-‘) << setw(80) << “-” << endl
<< “Rent-A-Vehicle” << endl << setfill(‘-‘)
<< setw(80) << “-” << endl;

/* Display Items */
cout << “[1] Read data from file” << endl;
cout << “[2] Print out all data for all cars” << endl;
cout << “[3] Estimate car rental cost” << endl;
cout << “[4] Find most expensive car” << endl;
cout << “[5] Print out all available cars” << endl;
cout << “[6] Exit program” << endl;
cout << setfill(‘-‘) << setw(80) << “-” << endl;

/* Get Menu Selection */
select(selection);

rentalCar cars[CAR_COUNT]; // create array of cars

switch (selection) {
case 1:
readFile(cars); // run readFile function and supply the cars array
break;
case 2:
printCars(false, cars); // run printCars function
break;
case 3:
estimatePrice(cars); // run estimatePrice function and supply the cars array
break;
case 4:
mostExpensive(cars); // run mostExpensive function and supply the cars array
break;
case 5:
printCars(true, cars); // run printCars function
break;
case 6:
cout << “Exiting program” << endl; // notify user that the program is exiting
break;
default:
cout << “Incorrect input” << endl; // notify user that the input is wrong
}

} while (selection != 6); // continue loop as long as user did not choose to exit

return 0;
}

void select(int &selection) {
cout << “Selection: “;
cin >> selection; // get user input and save it to selection variable
cout << setfill(‘-‘) << setw(80) << “-” << endl;
}

void readFile(rentalCar cars[]) {
ifstream fin; // Create input stream variable
char ifileName[20]; // Create var to hold input file name
cout << “Enter input file name: “;
cin >> ifileName; // Save user input to file name
fin.open(ifileName); // open user-definied file
if (fin.is_open()) // Check if file opened correctly
{
for (int i = 0; i < CAR_COUNT; i++) { // loop through each car in cars array
/* Populate cars array with data from user-defined file */
fin >> cars[i].year >> cars[i].make >> cars[i].model
>> cars[i].price >> cars[i].available;
}
cout << “Data from ” << ifileName << ” read successfully” << endl;

} else { cout << “Failed to open file named ” << ifileName; }
}

void printCars(bool availableOnly, rentalCar cars[]) {
if (availableOnly != true) { // check availableOnly flag
cout << “All data for all cars:” << endl;
} else {
cout << “All data for available cars:” << endl;
}

/* Print out table labels */
cout << setfill(‘~’) << setw(51) << “~” << endl
<< setfill(‘ ‘) << left << setw(8) << “YEAR” << setw(10) << “MAKE”
<< setw(11) << “MODEL” << setw(13) << “PRICE/DAY” << “AVAILABLE” << endl
<< setfill(‘~’) << setw(51) << “~” << endl;

for (int i = 0; i < CAR_COUNT; i++) { // loop through each car in cars array
/* If availableOnly is true, make sure to only print cars with available set to true */
if (cars[i].available != false || availableOnly != true) {
/* Print and style each car’s data */
cout << setfill(‘ ‘) << left << setw(8) << cars[i].year
<< setw(10) << cars[i].make << setw(11) << cars[i].model
<< ‘$’ << setw(12) << cars[i].price << setw(7)
<< boolalpha << cars[i].available << endl;
}
}
}

void estimatePrice(rentalCar cars[]) {
int carNumber; // create var to hold car number choice
int dayCount; // create var to hold day count choice
cout << “Enter car number (1-10): “;
cin >> carNumber; // get user input and save to carNumber
cout << “Enter number of days you want to rent: “;
cin >> dayCount;// get user input and save to dayCount

float cost = cars[carNumber-1].price*dayCount; // calculate final cost
cout << “Your estimated total cost is: $” << cost << endl;
}

void mostExpensive(rentalCar cars[]) {
int mostExpensiveIndex = 0; // Index of the most expensive car
for (int i = 0; i < CAR_COUNT; i++) { // loop through each car in cars array
if (cars[i].price > cars[mostExpensiveIndex].price) { // if this price is bigger than the biggest..
mostExpensiveIndex = i; // set the biggest to this
}
}

cout << “Most expensive car: ”
<< cars[mostExpensiveIndex].year << ” ”
<< cars[mostExpensiveIndex].make << ” ”
<< cars[mostExpensiveIndex].model << endl;
}

=========================================

car.txt

2012 Honda CRV 85.10 0
2014 Toyota Tacoma 115.12 1
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1

============================================

sample output:

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.

× How can I help you?