In: Computer Science
Hi, Im not sure why this only continues to loop when option 1 is selected.
Thank you
#include<iostream>
using namespace std;
class Car{
private:
string make;//variable
int price;//variable
public:
Car(string make,
int price){//two parameter constructor
this->make = make;
this->price = price;
}
string getMake(){//get make from
user
return
this->make;
}
int getPrice(){//get price from
user
return
this->price;
}
};
#include <iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<iomanip>
#include"Car.h"
using namespace std;
void DisplayMenu(){//method to display menu
cout<<"\n1. Add car to inventory";
cout<<"\n2. Delete car from inventory";
cout<<"\n3. Print inventory";
cout<<"\n4. Exit";
cout<<"\nYour choice: ";
}
void addData(vector<Car>& Inventory){//method to add
data
string make;
int price;
cout<<"Type of car: ";
cin>> make;
cout<<"Price of car: ";
cin>> price;
Car newcar(make, price);
Inventory.push_back(newcar);
}
void deleteData(vector<Car>& Inventory){//method to
delete data
string carToDelete;
cout<<"Type of car to delete: ";
cin>>carToDelete;
int position = -1;
for(int i = 0; Inventory.size(); i++){
if(carToDelete.compare(Inventory[i].getMake()) ==0)
position = i;
}
if(position == -1){
cout<<"No such car in inventory";
return;
}
Inventory.erase(Inventory.begin() + position );
}
void printData(vector<Car>& Inventory){//method to
print data
cout<<"\nMake"<<" Price"<<"\n";
cout<<"----"<<" -----\n";
for(int i = 0; i<Inventory.size();
i++){
cout<<setw(0)<<Inventory[i].getMake()<<setw(11)<<Inventory[i].getPrice()<<endl;
}
}
int main() {
cout<<"Welcome to the JJC Dealership Inventory
Program\n";
vector<Car>Inventory;//initialize vector
int choice = 1;//variable
while(true){
DisplayMenu();//display menu
cin>>choice; //get user
choice
switch(choice){//switch statement
case 1:
addData(Inventory);
break;
case 2:
deleteData(Inventory);
break;
case 3:
printData(Inventory);
case 4:
return 0;
break;
default:
cout<<"Invalid Entry\n";
break;
}
}
return 0;
}
The break statement is missing in the case 3 option. This is making the program to loop continuously in some cases. Please see the updated main function below.
Please note that I have not made any changes to the overall functionality of the program.
int main() {
cout << "Welcome to the JJC Dealership Inventory Program\n";
vector<Car>Inventory;//initialize vector
int choice = 1;//variable
while (true) {
DisplayMenu();//display menu
cin >> choice; //get user choice
switch (choice) {//switch statement
case 1:
addData(Inventory);
break;
case 2:
deleteData(Inventory);
break;
case 3:
printData(Inventory);
break;
case 4:
return 0;
break;
default:
cout << "Invalid Entry\n";
break;
}
}
return 0;
}
Please also make this change in the delete functionality. The terminating condition was not proper in the for loop in the deleteData function. It should be "for (int i = 0; i < Inventory.size(); i++)". Please see the code of the deleteData function below.
void deleteData(vector<Car>& Inventory) {
string carToDelete;
cout << "Type of car to delete: ";
cin >> carToDelete;
int position = -1;
for (int i = 0; i < Inventory.size(); i++) {
if (carToDelete.compare(Inventory[i].getMake()) == 0)
position = i;
}
if (position == -1) {
cout << "No such car in inventory";
return;
}
Inventory.erase(Inventory.begin() + position);
}
Please see the screenshots of the modified code and the output.