Question

In: Computer Science

Hi, Im not sure why this only continues to loop when option 1 is selected. Thank...

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;
}

Solutions

Expert Solution

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.


Related Solutions

Is the Federal Register a reliable resource? Why? (im not sure) ​is the Medicare & Medicaid...
Is the Federal Register a reliable resource? Why? (im not sure) ​is the Medicare & Medicaid Research Review a reliable resource? Why? yes, it is a well-known federal agency that is within the U.S. Department of Health and Human Services is the Health Affairs a reliable a reliable resource? why? (im not sure) ​is the Journal of Health Economics a reliable resource? why? (im not sure)
Im not sure if im asking it correctly but 1.how does emerging makerts affect increase standard...
Im not sure if im asking it correctly but 1.how does emerging makerts affect increase standard of living in Turkey. 2. how does emerging markets in turkey affect increased incomes? 3. how does the increase or decrease in gdp lead to economic growth in turkey in an emerging market.
Please answer either one (option 1 or 2) thank you Option 1: A Communication Skill Needed...
Please answer either one (option 1 or 2) thank you Option 1: A Communication Skill Needed to Play a Role Define a role that you want to do better. Perhaps it is getting a better position at work, becoming a supervisor at . . .? Perhaps it’s to be a better student (one who gets better grades). For the role, be sure to define it (just a sentence or two). For example, you could describe the key duties and responsibilities...
Please show your work - the answer is d) but I'm not sure why. Thank you!...
Please show your work - the answer is d) but I'm not sure why. Thank you! The time it takes to complete a Sta220 term test is normally distributed with a mean of 100 minutes with standard deviation of 14 minutes. How much time should be allowed if we wish to ensure that at least 9 out of 10 students (on average) can complete it? (round to the nearest minute) A) 115 B) 116 C) 117 D) 118 E) 119
Does "current travels the path of least resistance" only apply in a single loop (when there...
Does "current travels the path of least resistance" only apply in a single loop (when there is no node) ? For instance, if there is a node that current one enters, current 2 and current 3 exist the node; If "current travels in the path of least resistance" why doesn't all the current exit through the the side of the node that has the least resistance?
Share with the class why you have selected these metrics, AND be sure to describe how...
Share with the class why you have selected these metrics, AND be sure to describe how you will gather data for the metrics. Select three metrics that you will use in your final paper to measure the effectiveness of the HR programs at the end of the transformation you will design. The three metrics to measure the effectiveness of the HR programs at the end of transformation that I have designed are: 1. Staff engagement- Staff engagement is pertinent for...
Please answer the below, all parts! Thank you in advance. Option 1: Think of a problem...
Please answer the below, all parts! Thank you in advance. Option 1: Think of a problem dealing with two possibly related variables (Y and X) that you may be interested in. Share your problem and discuss why a regression analysis could be appropriate for this problem. Specifically, what statistical questions are you asking? Why would you want to predict the value of Y? What if you wanted to predict a value of Y that’s beyond the highest value of X...
Please, I need correct answers. Thank you, Short Answers 1-Why does it matter how and when...
Please, I need correct answers. Thank you, Short Answers 1-Why does it matter how and when a company recognizes revenue? 2-Why is determining when to recognize revenue more difficult under accrual accounting vs. cash accounting?
Hi anyone can answar these questions or only part of them ? 1-What are the advantages...
Hi anyone can answar these questions or only part of them ? 1-What are the advantages of using thionyl chloride rather than other available chlorination agents? 2-When an oil bath is used to reflux a solution, why is it recommended ? 3-If charcoal is used in a recrystallization, how and why might Celite then be used subsequently? 4-Why is thionyl chloride removed by distillation at reduced pressure rather than at atmospheric pressure? 5-Illustrate the mechanism for the reaction of thionyl...
1.) Why do is the overall cost of capital used for investment decisions even when only...
1.) Why do is the overall cost of capital used for investment decisions even when only one source of capital will be used (e.g., debt)? 2.) In computing the cost of capital, are the historical costs of existing debt and equity or the current costs as determined in the market used? Why? 3.) Why is the cost of retained earnings equal to the firm's required rate of return on its common stock (Ke)? 4.) If the company has the opportunity...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT