15,895,370 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by Sanket Awasthy (Top 4 by date)
Sanket Awasthy
10-Mar-23 23:10pm
View
there are many errors in this code please resolve these errors i was not able to fix these errors by myself
Sanket Awasthy
10-Mar-23 23:09pm
View
cin >> year;
cout << "Enter the odometer reading of the vehicle: ";
cin >> odometer;
library.updateVehicle(index, make, model, year, odometer);
break;
}
case 4:
library.removeVehicle();
break;
case 5:
library.tireRotation();
break;
case 6:
library.oilChange();
break;
case 7:
library.recharge();
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (userInput != 0);
return 0;
}
Sanket Awasthy
10-Mar-23 23:08pm
View
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
int vehicleIndex;
cout << "Enter the index of the vehicle to remove: ";
cin >> vehicleIndex;
if (vehicleIndex < 1 || vehicleIndex > inventory_.size()) {
cout << "Invalid vehicle index.\n";
return;
}
inventory_.erase(inventory_.begin() + vehicleIndex - 1);
cout << "Vehicle removed from inventory.\n";
}
void tireRotation() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
for (int i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
ElectricCar* electricCar = dynamic_cast<electriccar*>(vehicle);
if (electricCar != nullptr) {
electricCar->checkTireRotation();
}
}
}
void oilChange() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
for (int i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
GasCar* gasCar = dynamic_cast<gascar*>(vehicle);
DieselCar* dieselCar = dynamic_cast<dieselcar*>(vehicle);
if (gasCar != nullptr || dieselCar != nullptr) {
int odometer = vehicle->getOdometer();
if (odometer % 10000 == 0) {
cout << "Vehicle " << i << " needs an oil change.\n";
}
}
}
}
void recharge() {
if (inventory_.empty()) {
cout << "Inventory is empty.\n";
return;
}
for (int i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
ElectricCar* electricCar = dynamic_cast<electriccar*>(vehicle);
if (electricCar != nullptr) {
electricCar->checkPower();
}
}
}
};
vector<vehicle> inventory;
int main() {
MaintenanceLibrary library;
int userInput;
do {
cout << "Menu:\n";
cout << " 0. Quit.\n";
cout << " 1. List inventory.\n";
cout << " 2. Add vehicle.\n";
cout << " 3. Update vehicle.\n";
cout << " 4. Remove vehicle.\n";
cout << " 5. Tire rotation.\n";
cout << " 6. Oil change.\n";
cout << " 7. Recharge.\n";
cout << "Enter your choice: ";
cin >> userInput;
switch (userInput) {
case 0:
cout << "Exiting program.\n";
break;
case 1:
library.listInventory();
break;
case 2: {
string make, model;
int year, odometer;
cout << "Enter vehicle make: ";
cin >> make;
cout << "Enter vehicle model: ";
cin >> model;
cout << "Enter vehicle year: ";
cin >> year;
cout << "Enter vehicle odometer reading: ";
cin >> odometer;
library.addVehicle(new Vehicle(make, model, year, odometer));
break;
}
case 3: {
int index;
string make, model;
int year, odometer;
cout << "Enter the index of the vehicle to update: ";
cin >> index;
cout << "Enter the make of the vehicle: ";
cin >> make;
cout << "Enter the model of the vehicle: ";
cin >> model;
cout << "Enter the year of the vehicle: ";
Sanket Awasthy
10-Mar-23 23:07pm
View
#include <iostream>
#include <string>
#include <vector>
using namespace std;
enum class FuelType {
PETROL,
DIESEL,
GAS,
ELECTRIC
};
class Vehicle {
protected:
string make_;
string model_;
int year_;
int odometer_;
public:
Vehicle(string make, string model, int year, int odometer)
: make_(make), model_(model), year_(year), odometer_(odometer) {}
string getMake() const { return make_; }
void setMake(string make) { make_ = make; }
string getModel() const { return model_; }
void setModel(string model) { model_ = model; }
int getYear() const { return year_; }
void setYear(int year) { year_ = year; }
int getOdometer() const { return odometer_; }
void setOdometer(int odometer) { odometer_ = odometer; }
virtual FuelType getFuelType() = 0;
};
//abstract class - containing one pure virtual method
class FuelVehicle : public Vehicle
{
public: //access specifier
int quarts; //data member
void checkOil() //member function
{
cout<<endl<<" enter="" oil="" level="" (in="" quarts)="" :="" ";
="" cin="">>quarts;
if(quarts <= 1)
{
cout<<endl<<" engine="" oil="" level="" :="" low";
="" }
="" else
="" cout<<endl<<"="" is="" fine";
="" friend="" void="" oilchange(fuelvehicle="" &oil);="" function="" declaration
};
class="" gascar="" public="" vehicle="" {
public:
="" gascar(string="" make,="" string="" model,="" int="" year,="" odometer)
="" vehicle(make,="" odometer)="" {}
="" fueltype="" getfueltype()="" override="" {="" return="" fueltype::gas;="" }
};
class="" dieselcar="" dieselcar(string="" fueltype::diesel;="" electriccar="" electriccar(string="" fueltype::electric;="" }
="" checktirerotation()="" {
="" if="" (odometer_="" %="" 50000="=" 0)="" cout="" <<="" "it's="" time="" for="" a="" tire="" rotation."="" endl;
="" }="" else=""> 0) {
cout << "You should rotate your tires soon." << endl;
}
}
void checkPower() {
if (odometer_ % 400 == 0) {
cout << "Your car needs to be recharged." << endl;
} else {
cout << "Your car is good to go." << endl;
}
}
};
class MaintenanceLibrary {
private:
vector<vehicle*> inventory_;
public:
void addVehicle(Vehicle* vehicle) {
cout << "Enter the make of the vehicle: ";
cin >> vehicle->make_;
cout << "Enter the model of the vehicle: ";
cin >> vehicle->model_;
cout << "Enter the year of the vehicle: ";
cin >> vehicle->year_;
cout << "Enter the odometer reading of the vehicle: ";
cin >> vehicle->odometer_;
inventory_.push_back(vehicle);
cout << "Vehicle added to inventory.\n";
}
void listInventory() {
if (inventory_.empty()) {
cout << "The inventory is empty." << endl;
return;
}
cout << "Inventory:" << endl;
for (size_t i = 0; i < inventory_.size(); i++) {
Vehicle* vehicle = inventory_[i];
cout << i << ". " << vehicle->getMake() << " " << vehicle->getModel() << " (" << vehicle->getYear() << ")" << endl;
}
}
void updateVehicle(int index, string make, string model, int year, int odometer) {
if (index >= inventory_.size()) {
cout << "Vehicle not found." << endl;
return;
}
Vehicle* vehicle = inventory_[index];
vehicle->setMake(make);
vehicle->setModel(model);
vehicle->setYear(year);
vehicle->setOdometer(odometer);
}
void removeVehicle() {