Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This must be coded in c++. Thank you! This is a sampe input file called Cars.txt

ID: 3882578 • Letter: T

Question

This must be coded in c++. Thank you!

This is a sampe input file called Cars.txt

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 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 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 (1-true; 0 = false; try to display true/ false using the "std: :boolalpha" manipulator like: cout

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;

void copy (char p[10] , char q[10]){
   for (int i = 0; i<10; i++)
       p[i] = q[i];
  
}

struct car{
int year;
char make[10];
char model[10];
float price;
bool available;
};

void sort(struct car data[],int n){
    struct car temp;

    for (int i = 0; i<n; i++){
        for (int j =i+1; j<n; j++){
            if (data[i].price > data[j].price){
                temp.year = data[i].year;
                copy(temp.make,data[i].make);
                copy(temp.model,data[i].model);
                temp.price = data[i].price;
                temp.available = data[i].available;
                data[i].year = data[j].year;
                copy(data[i].make,data[j].make);
                copy(data[i].model,data[j].model);
                data[i].price = data[j].price;
                data[i].available = data[j].available;
                data[j].year = temp.year;
                copy(data[j].make,temp.make);
                copy(data[j].model,temp.model);
                data[j].price = temp.price;
                data[j].available = temp.available;

            }
        }
    }
}


void print(struct car data[],int n){
   for (int i = 0; i<n; i++){
      cout << data[i].year << " " << data[i].make << " " << data[i].model << "," << "$" << data[i].price << "," << "Availability = " ;
      if (data[i].available == 0)
          cout << "false" << endl;
      else
          cout << "true" << endl;
   }

}


void printEstimate(struct car data[],int n, int num){
  
   for (int i = 0; i<n; i++){
      if (data[i].available == 1){
         cout << data[i].year << " " << data[i].make << " " << data[i].model << "," << "Total Cost : $" << data[i].price * num << endl;
      }
   }

}

void input(struct car data[]){
   char filename[20];

   ifstream fin;
   char avail[7];

   cout << "Enter filename :" << endl;
   cin >> filename;
   fin.open("Cars.txt");
   int count = 0;
   while (fin >> data[count].year >> data[count].make >> data[count].model >> data[count].price >> avail){
      
       if (avail[0] == 'f')
          data[count].available = false;
       else
          data[count].available = true;
       count++;
   }
}


void output(struct car data[], int n){
   char filename[20];

   ofstream fout;


   cout << "Enter filename :" << endl;
   cin >> filename;
   fout.open(filename);
   int count = 0;
   for (int i = 0; i<n; i++){
      fout << data[i].year << " " << data[i].make << " " << data[i].model << " " << data[i].price << " ";
      if (data[i].available == false){
          fout << "false" << endl;

      }
      else{
          fout << "true" << endl;

      }
      
   }
}

void options(struct car data[],int n){
    
     int num_days;
     sort(data,5);
     cout << "How many days : " ;
     cin >> num_days;
     printEstimate(data,n,num_days);

     
}

void rent(struct car data[], int n){

   int index;
   int num;


   cout << "Enter car and number of days :" ;
   cin >> index >> num;

   if (data[index-1].available == true){
       data[index-1].available = false;
       cout << "Rent is success" << endl;
   }
   else {
       cout << "Car is not avaiable" << endl;
   }

}


int main(){

   struct car data[5];
   int choice;

   do {
       cout << "1.Input filename ";
       cout << "2.Print all to terminal ";
       cout << "3.Print all to separate file ";
       cout << "4.Sort ";
       cout << "5.How many days ";
       cout << "6.Which car ";
       cout << "7.Which car ";
       cout << "Enter your choice: ";
       cin >> choice;
    
       switch (choice){

            case 1: input(data);
                    break;
            case 2: print(data, 5);
                    break;
            case 3: output(data,5);
                    break;
            case 4: sort(data,5);
                    break;
            case 5: options(data,5);
                    break;
            case 6: rent(data,5);
                    break;

       }
      
   }while (choice != 7);
  
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote