Question

In: Computer Science

The program is to be called CarClub The application will display menu as below 1. load...

The program is to be called CarClub

The application will display menu as below

1. load cars

2. display all cars

3. search car

4 count cars older the 30 years

5 exit

Please enter your option:

Load car menu should read input data from text file name (car.txt)

Car class have 4 data members which are. Car make, car model, car year and car price

In addition

If both car year and price were not provided, year has a default value as 2020 and price has a default value as $20000

If only price was not provided, price should be set to a default value based on this table

2015<year<=2020 and price has a default value as $20000

2010<year<=2015 and price has a default value as $14000

1980<year<=2010 and price has a default value as $7000

2015<year<=2020 and price has a default value as $20000

year<=1980 and price has a default value as $40000

Using dynamic memory allocation to store Car Object

You should implement at least 1 custom excepetion and 1 file exception

You have to implement 1 operator overloading for operator "<<" to use as a print

WHY IS BUILD FAILED?????

#include

#include

using namespace std;

#include

class Ccar

{

private:

string make;

string model;

int year;

double price;

  

  

public:

  

Ccar(string="", string="", int=2020, double=20000);

~Ccar();

  

// setters

void setMake(string);

void setModel(string);

void setYear(int);

void setPrice(double);

  

// getters

string getMake();

string getModel();

int getYear();

double getPrice();

};

void Ccar:: setMake(string ma){

ma=make;

}

void Ccar :: setModel(string m){

m=model;

  

}

void Ccar :: setYear(int y){

y=year;

  

}

void Ccar :: setPrice(double p){

p=price;

}

// getters

string Ccar::getMake()

{

return make;

}

string Ccar::getModel()

{

return model;

}

int Ccar::getYear()

{

return year;

}

double Ccar::getPrice()

{

return price;

}

int main() {

int option, i, numberOffCars, year = 0;

string line, make, model, yearString, priceString;

double price;

bool loaded =false;

  

ifstream fin;

// file exception to be typed here

// num of cars counted

fin.open("cars.txt");

numberOffCars=0;

while (fin.good()) //while i have not reached the eof

{

getline(fin, line);

numberOffCars++;

}

fin.close();

//memory allocation

Ccar *carList = new Ccar[numberOffCars]; // Dynamic memory allocation

//menu

do{

cout <<"1. Load Cars"<

cout <<"2. Display all Cars"<

cout <<"3. Search Cars"<

cout <<"4. Count Cars older than 30 years"<

cout <<"5. Exit"<

cin>>option;

if (option <1 || option >5)

{

cout<<"error"<

else

{

switch (option)

{case 1: // load

loaded=true;

fin.open("car.txt");

i=0;

while (fin.good())

{

yearString="";

priceString="";

getline(fin, make,'$');

carList[i].setModel(model);

getline(fin, yearString,'$');

if (yearString!="")

year= stoi(yearString);

  

getline(fin, priceString,'$');

if (priceString!="")

price= stod(priceString);

if (yearString=="" && priceString =="")

{

carList[i].setYear(2020);

carList[i].setPrice(20000);

  

}

else if (priceString=="")

{

if (year>2015 && year<=2020)

carList[i].setPrice(20000);

else if (year>2010 && year<=2015)

carList[i].setPrice(14000);

else if (year>1980 && year<=2010)

carList[i].setPrice(7000);

else

carList[i].setPrice(400000);

}

i++; //next car

} //end of while

fin.close();

break;

case 2: // display

if (loaded)

{i=0;

while(i

{cout <<"carList[i]"<

i++; //next car

}

}

break;

case 3: // search

if (loaded)

{

cout << "Enter a car make: "<< endl;

cin>>make;

cout << "Enter a car model: "<< endl;

cin>>model;

// search for this make and model

}

else {

cout << "Load the cars first! "<< endl;

  

}

  

break;

  

case 4: // count the cars older than 30 years

  

break;

  

case 5: // exit

  

cout << "\n\n\t\t\t Thanks for using our aplication"<< endl;

  

default: // already handled by cust excep

cout << "Invalid option! Try again"<< endl;

break;

}}} // end of switch

  

  

while (option !=5); // 5 exit option

delete[] carList;

return 0;

}

  

Solutions

Expert Solution

Hey, i can see there are some mistakes in the program. It would be difficult for you to understand by mentioning one by one since there is no numbers for the code lines so I'll just type all the code :

include <iostream>
using namespace std;
#include<string>

#ifndef _CAR_
#define _CAR_

class Ccar
{
private:
string make;
string model;
int year;
double price;


public:

Ccar(string="", string="", int=2020, double=20000);
~Ccar();

// setters
void setMake(string);
void setModel(string);
void setYear(int);
void setPrice(double);

// getters
string getMake();
string getModel();
int getYear();
double getPrice();

friend ostream& operator<<(ostream& out, Ccar& param);
};

#endif

#include "Ccar.h"

void Ccar:: setMake(string){
make=make;
}
void Ccar :: setModel(string){
model=model;
  
}

void Ccar :: setYear(int y){
y=year;
  
}

void Ccar :: setPrice(double p){
p=price;
}

// getters

string Ccar::getMake()
{
return make;
}
string Ccar::getModel()
{
return model;
}

int Ccar::getYear()
{
return year;
}
double Ccar::getPrice()
{
return price;
}

#include <exception>
using namespace std;
class myexception: public exception{
// exception implementation???
};

#include <fstream>
#include "Ccar.h"

int main() {
int option, i, numberOffCars, year = 0;
string line, make, model, yearString, priceString;
double price;
bool foundIt, loaded =false;
exception myex;
ifstream fin;
// file exception to be typed here
  
// num of cars counted
fin.open("car.txt");
numberOffCars=0;
while (fin.good()) //while i have not reached the eof
{
getline(fin, line);
numberOffCars++;
}
fin.close();
  
//memory allocation
try {
Ccar *carList = new Ccar[numberOffCars]; // Dynamic memory allocation
//menu
do{
cout <<"1. Load Cars"<<endl;
cout <<"2. Display all Cars"<<endl;
cout <<"3. Search Cars"<<endl;
cout <<"4. Count Cars older than 30 years"<<endl;
cout <<"5. Exit"<<endl;
cin>>option;
try {
if (option <1 || option >5)
{
throw myex;
}
else
{
switch (option)
{case 1: // load
loaded=true;
fin.open("car.txt");
i=0;
while (fin.good())
{
yearString="";
priceString="";
getline(fin, make,'$');
carList[i].setModel(model);
getline(fin, yearString,'$');
if (yearString!="")
year= stoi(yearString);
  
getline(fin, priceString,'$');
if (priceString!="")
price= stod(priceString);
if (yearString=="" && priceString =="")
{
carList[i].setYear(2020);
carList[i].setPrice(20000);
  
}
else if (priceString=="")
{
if (year>2015 && year<=2020)
carList[i].setPrice(20000);
else if (year>2010 && year<=2015)
carList[i].setPrice(14000);
else if (year>1980 && year<=2010)
carList[i].setPrice(7000);
else
carList[i].setPrice(400000);
}
i++; //next car

} //end of while
fin.close();
break;
case 2: // display
if (loaded)
{i=0;
while(i <numberOffCars)
{cout <<carList[i]<<endl;
i++; //next car
}
}
break;
case 3: // search
if (loaded)
{
cout << "Enter a car make: "<< endl;
cin>>make;
cout << "Enter a car model: "<< endl;
cin>>model;
// search for this make and model
}
else {
cout << "Load the cars first! "<< endl;
  
}
  
break;
  
case 4: // count the cars older than 30 years
  
break;
  
case 5: // exit
  
cout << "\n\n\t\t\t Thanks for using our aplication"<< endl;
  
default: // already handled by cust excep
cout << "Invalid option! Try again"<< endl;
break;
} // end of switch
  
} // end of try
  
} catch (exception& ex){
cout << ex.what() << endl;
}
}
while (option !=5); // 5 exit option
delete[] carList;
}
catch (bad_alloc&)
{
cout << "Error in allocating memory" <<endl;
}
return 0;
}

Hey, hope this helps u,if so give a thumbs up, it means a lot thanks:) In case of any problem, write a comment I'll sure be helping you. Have a good day:)


Related Solutions

Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
Java Create a method to display a menu. When this method is called it should receive...
Java Create a method to display a menu. When this method is called it should receive the user's name, great the user, and ask them to make a selection. 1 - Change your name, 2 - Test your IQ, 3 - Display a table, 4 - Play a game, 5 - Exit.
In this program you will develop an application that will display an amortization schedule.
Using Anaconda Python, program the following:OverviewIn this program you will develop an application that will display an amortization schedule. The word "amortize" means to "To write off gradually and systematically a given amount of money within a specific number of time periods." It will show, for each month of the loan, how much of your monthly loan payment is being applied towards interest costs, and how much is actually being applied to reduce the outstanding balance (principal) of your loan.The...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
We need to create basic program that will simply display a menu and allow the user...
We need to create basic program that will simply display a menu and allow the user to select one of the choices. The menu should be displayed such that each option will be numbered, as well as have one capital letter so as to indicate that either the number or the designated letter can be entered to make their choice. Once the choice is made, the sub-menu for that selection should be displayed. Colors with an odd number of letters...
Write a java program that will first display the following menu: Choose one of the following...
Write a java program that will first display the following menu: Choose one of the following 1- To add 2 double integers 2- To add 2 integer numbers 3- To add 3 double numbers 4- To add 3 integer numbers After reading user’s choice, use a switch case statement to call the corresponding method Void add 1 (double n1, double n2) Void add2() Double add3 (double n1, double n2, double n3) Double add4 ()
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names     &
Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close Do you...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                       ...
C++ PLEASE Write a program to prompt the user to display the following menu: Guess-Number                        Concat-names             Quit If the user selects Guess-number, your program needs to call a user-defined function called int guess-number ( ). Use random number generator to generate a number between 1 – 100. Prompt the user to guess the generated number and print the following messages. Print the guessed number in main (): Guess a number: 76 96: Too large 10 Too small 70 Close...
Console ================================================================                        Baseball Team Manager MENU OPTIONS 1 – Display
Console ================================================================                        Baseball Team Manager MENU OPTIONS 1 – Display lineup 2 – Add player 3 – Remove player 4 – Move player 5 – Edit player position 6 – Edit player stats 7 - Exit program POSITIONS C, 1B, 2B, 3B, SS, LF, CF, RF, P ================================================================ Menu option: 1 Player POS AB H AVG ---------------------------------------------------------------- 1 Denard OF 545 174 0.319 2 Joe 2B 475 138 0.291 3 Buster C 535 176 0.329 4 Hunter OF 485...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT