Question: I need help with this project, I am posting my code below the description of the project, I need help writing it without global variables
I need help with this project, I am posting my code below the description of the project, I need help writing it without global variables and without using std libraries. I'm also posting an issue I have with my source code outputting and extra car that I don't know how to get rid of. thanks for the help!






#include
// Defines a structure to store car information struct RentalCar { // Data member to store data int year; char make[MAXCHAR]; char model[MAXCHAR]; float price; bool available; };// End of structure
// Function to return the length of a string size_t myStringLength(const char *str) { // Initializes length to zero int len = 0; // Loops till end of the string for(; str[len] != '\0'; len++) ; // Null statement // Returns length return len; }// End of function
int myStringCompare(const char *str1, const char *str2) { // Calls the function to check the length of the two strings // If first string length is greater than the second string length then return -1 if(myStringLength(str1) > myStringLength(str2)) return -1;
// Loops till end of the first string for(int x = 0; str1[x] != '\0'; x++) { // Checks if current character of the first string is greater than the second string // Return one if(str1[x] > str2[x]) return 1; // Otherwise checks if current character of the first string is less than the second string // Return minus one else if(str1[x]
// Function to read file contents and stores it in RentalCar array of object int readData(RentalCar rc[]) { // To store file name char fileName[MAXCHAR]; // Counter for number of records. Initializes to zero int counter = 0; // To store availability status char ava[5]; // Declare ifstream object ifstream inFile;
// Accepts the file name cout>fileName;
// Open file for reading inFile.open(fileName);
// Checks whether file can be opened or not if(inFile.fail()) { cout
// Loops till not end of file while (!inFile.eof()) { // Reads year inFile>>rc[counter].year; // Reads make inFile>>rc[counter].make; // Reads model inFile>>rc[counter].model; // Reads price inFile>>rc[counter].price; // Reads availability inFile>>ava; // Calls the function to check if it is true if(myStringCompare(ava, "true") == 0) // Stores one rc[counter].available = 1; // Otherwise stores zero else rc[counter].available = 0; // Increase the record counter by one counter++; }// End of while loop
// Returns number of records return counter; }// End of function
// Function to write records to file void writeData(RentalCar rc[], int counter) { // Declares an object of class ofstream ofstream outFile; // To store file name char fileName[MAXCHAR];
// Accepts the file name cout>fileName;
// Open file for writing outFile.open(fileName);
// Checks whether file can be opened or not if(outFile.fail()) { cout
// Loops till number of records minus one for(int x = 0; x
// Function to display all the cars information void display(RentalCar rc[], int len) { // Loops number of records for(int x = 0; x
// Function to sort the car ascending order of price void mySort(RentalCar rc[], int len) { // Temporary object created for swapping RentalCar temp; // Loops number of records minus one times for (int r = 0; r rc[c + 1].price) { // Swapping process temp = rc[c]; rc[c] = rc[c + 1]; rc[c + 1] = temp; }// End of if condition }// End of inner for loop }// End of outer for loop }// End of function
// Function to check the availability of the car void available(RentalCar rc[], int len) { int no, p = 0; // Creates an array of objects to store available cars RentalCar temp[MAX]; // Accepts number of days to rent cout>no;
// Loops number of records for (int x = 0; x
// Loops till number of available cars for (int x = 0; x
// Function to rent a car void rentCar(RentalCar rc[], int len) { int no, index; // Accepts the car index cout>index; // Accepts number of days to rent cout>no;
// Checks if the car is not available display error message if(rc[index].available == false) { cout
// Otherwise else { // Sets the car availability to false rc[index].available == false; // Display the rent cout
// Function to accept user choice and return it int menu() { // To store user choice int ch; // Displays menu cout>ch; // Returns user choice return ch; }// End of function
// main function definition int main() { // Creates an array of object of RentalCar of size MAX RentalCar rc[MAX]; int len; // Loops till user choice is not 7 do { // Calls the function to receive user choice and calls the appropriate function based on user choice switch(menu()) { case 1: len = readData(rc); break; case 2: display(rc, len); break; case 3: writeData(rc, len); break; case 4: mySort(rc, len); cout 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-strings is also included. Description: or 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 exac following the same data layout), 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 User 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. entries, each 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 (1 true; 0 false; try to display true/false using the "std: :boolalpha" manipulator like: cout 4) Sort the cars (i.e. sort the array of structs used to store all data) by ascending price 5) Ask the user for how many days they want to rent a car. Then print to the terminal
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
