The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 304 Weeks Ago, 2 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
Hello,
This is for my CS202 class where we are coding in c++. I am having a difficult time with just printing out ALL data for all cars. I know my readFile function needs adjustment, I just do not know how.
Â
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 10 different cars, 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, 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:
-Â make, a C-style string
-Â model, a C-style string
-Â year, an int
-Â price, a float (price per day)
-Â available, a bool (1 = true; 0 = false; try to display true/false using the "boolalpha" flag, optional)
Â
The menu must have the following functionality:
- Read ALL data from file.
-Â Print out ALLÂ data for all of the cars.
- Estimate car rental cost - prompt the user for a car number (first car in file should be
represented to the user as #1), and the number of days to rent the car.
- Find the most expensive car.
- Print out ONLY the available cars.
- Exit program.
Â
The following minimum functionality and structure is required:
- Ask the user for the input file name.
- The list of cars must be stored in an array of structs.
- Use character arrays (i.e., C-style) to hold your strings. No string data type!
- Write multiple functions (Hint: each menu option should be a function).
- You can use pass by reference in your function.
- Write your own string copy, string compare (or other) functions as needed.
- The other functionality and structure of the program should remain the same as Project #1, including writing to screen and file and restrictions on string libraries, global variables and constants, etc.#include <iostream>
#include <fstream>
//using namespace std;
const int MAX_STRING_LENGTH = 50;
const int MAX_NUM_CARS = 10;
struct rentalCar
{
   char make[MAX_STRING_LENGTH];
   char model[MAX_STRING_LENGTH];
   int year;
   float price;
   bool availability;
};
// Function Prototypes
void readFile(char fileName[]);
void displayCarData(rentalCar cars);
void carEstimate(rentalCar car, int days);
void mostExpensive(rentalCar cars[]);
void availableCars(rentalCar cars[]);
int showMenu();
int main ()
{
   char nameOfFile[MAX_STRING_LENGTH];
   int menuChoice;
   bool programOn;
   rentalCar vehicles[MAX_NUM_CARS];
  Â
   std:: cout << "Enter file name: ";
   std:: cin >> nameOfFile;
  Â
   readFile(nameOfFile);
  Â
   do
   {
  Â
      menuChoice = showMenu();
     Â
      switch(menuChoice)
      {
         case 1:
            for(int i=0; i < MAX_NUM_CARS; i++)
            {
            std:: cout << i+1 << ") " ;
            displayCarData(vehicles[i]);
            }
            break;
        Â
         case 2:
            {
            int carNum = 0;
            int days = 0;
            std:: cout << "Enter car number" << std:: endl;
            std:: cin >> carNum;
            std:: cout << std:: endl;
            std:: cout << "Enter number of days" << std:: endl;
            std:: cin >> days;
            std:: cout << std:: endl;
            carNum--;
            carEstimate(vehicles[carNum], days);
            break;
            }
           Â
         case 3:
            mostExpensive(vehicles);
            break;
           Â
         case 4:
            break;        Â
           Â
         default:
            return 0;
            break;  Â
      }
   }while(programOn);
  Â
  Â
  Â
}
void readFile(char fileName[])
{
   std:: ifstream fileIn;
   rentalCar read;
  Â
   fileIn.open(fileName);
  Â
   //for(int i=0; i< MAX_NUM_CARS; i++)
   //{
      fileIn >> read.year;
      fileIn >> read.make;
      fileIn >> read.model;
      fileIn >> read.price;
      fileIn >> read.availability;
     Â
      fileIn>> read;
   //}
  Â
     Â
}
void displayCarData(rentalCar cars)
{Â Â Â
  Â
      std:: cout << cars.year << " "
           << cars.make << " "
           << cars.model << " "
           << cars.price << " per day "
           << "Available: ";
          Â
      if(cars.availability == true)
      {
         std:: cout << "true" << std:: endl;
      }
      else
      {
         std:: cout << "false" << std:: endl;
      }
  Â
}
void carEstimate(rentalCar car, int days)
{
   float cost = 0.0;
   cost = car.price * days;
   std:: cout << "Estimated cost of renting is: " << cost << std:: endl;
}
void mostExpensive(rentalCar cars[])
{
   float max = 0.0;
   int maxCar = 0;
   for(int i = 0; i < 10; i++) {
      if(cars[i].price > max) {
         max = cars[i].price;
         maxCar = i;
      }
   }
   std:: cout << "The most expensive car is: "
      << cars[maxCar].year << " "
      << cars[maxCar].make << " "
      << cars[maxCar].model << std:: endl;
}
void availableCars(rentalCar[])
{
  Â
}
int showMenu()
{
   int userInput;
  Â
   std:: cout << std:: endl << " Rental Car Service" << std:: endl;
   std:: cout << " ==================" << std:: endl << std:: endl;
  Â
   std:: cout << "1. Print data for all cars" << std:: endl;
   std:: cout << "2. Estimate car rental cost" << std:: endl;
   std:: cout << "3. Find the most expensive car" << std:: endl;
   std:: cout << "4. Print data for available cars" << std:: endl;
   std:: cout << "5. Exit program" << std:: endl << std:: endl;
   std:: cout << "Enter selection: " ;
  Â
   std:: cout << "Enter selection choice: " ;
   std:: cin >> userInput;
  Â
   std:: cout << std:: endl;
  Â
   return userInput;
}
----------- Â ----------- H-----------ell-----------o S-----------ir/-----------Mad-----------am ----------- Th-----------ank----------- yo-----------u f-----------or -----------you-----------r i-----------nte-----------res-----------t a-----------nd -----------buy-----------ing----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill----------- be----------- qu-----------ick-----------ly