I've been having some issues with vectors, specifically those with no size. I am making a program to add cars, and their prices to a list, and am unable to get the add data function to work properly. When printing the data, it does not print anything, and the vector itself is empty, because when trying to delete data, it's unable to find the items I entered. Here is my code:
main:
int main (void) {
vector<Car> carVect;
int input = 0;
do{
displayMenu();
cin >> input;
if (input == 1){
addData(carVect);
} else if (input == 2){
deleteData(carVect);
} else if (input == 3){
printData(carVect);
} else if (input == 4){
break; // breaks out of loop
} else {
cout << "invalid selection" << endl;
}
}
while (input != 4);
}
addData function:
bool addData(vector<Car>& carVect){
Car carObj;
cout << "Make of car, and price of car, separated by spaces: ";
cin >> carObj.carMake >> carObj.carPrice;
for (int i = 0; i < carVect.size(); i++) {
if (carVect[i].getCarMake().empty()){
carVect.push_back(carObj);
cout << "Car Entry Added" << endl;
return true;
}
}
cout << "There are no open Elements in the array" << endl;
}
Header file:
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string carMake;
int carPrice;
Car (string make,int price) {
carMake = make;
carPrice = price;
}
Car () { //default contructor
}
string getCarMake () {
return carMake;
}
int getCarPrice () {
return carPrice;
}
void setCarMake (string x){
carMake = x;
}
};
I understand that the vector doesn't have a size, but I would think that the first if statement in addData would be true, and push_back would give it an element with data in it. Any help would be appreciated.
Your vector
is initially empty, ie it has no Car
objects in it at all.
In addData()
, your loop is checking the vector's size()
, which will be 0, so the loop body will never be entered, and thus will not be able to find any "empty" Car
(which doesn't exist), and so will not push the new carObj
into the vector
at all.
Just get rid of the loop entirely, it is not doing you any good, eg:
bool addData(vector<Car>& carVect){
Car carObj;
cout << "Make of car, and price of car, separated by spaces: ";
if (cin >> carObj.carMake >> carObj.carPrice) {
carVect.push_back(carObj);
cout << "Car Entry Added" << endl;
return true;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Bad input!" << endl;
return false;
}
If, for some reason, you must use the loop, then you must pre-fill the vector
with default Car
objects, eg:
const int MaxCars = ...; // <-- desired count here
vector<Car> carVect(maxCars);
Then your loop will run correctly, and be able to find "empty" Car
s. However, when you do find an "empty" Car
, you are pushing the new carObj
into the end of the vector rather than replacing the "empty" Car that you found. Thus, the same "empty" Car
will always be found on subsequent calls to addData()
. So, you would need to get rid of the push_back()
to fix that, eg:
bool addData(vector<Car>& carVect){
for (int i = 0; i < carVect.size(); i++) {
if (carVect[i].getCarMake().empty()){
Car carObj;
cout << "Make of car, and price of car, separated by spaces: ";
if (cin >> carObj.carMake >> carObj.carPrice) {
//carVect.push_back(carObj);
carVect[i] = carObj;
cout << "Car Entry Added" << endl;
return true;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Bad input!" << endl;
return false;
}
}
cout << "There are no open Elements in the array" << endl;
return false;
}