-1

I am working on a car rental program, and in it I need to be able to access a vector of the cars and prices from another class in order to make the receipt. Except every time I try to access the vector, there is nothing in it. How can I access the vector with the data in it?

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
const string SPORTS_CARS = "sports.dat";
class InventoryItem
{
public: 
  InventoryItem() {};
  ~InventoryItem() {};
  friend class Cart;
  vector<string> listOfCars;
  vector<float> listOfPrices;
  void readSports()
  {
    string car;
    float price;
    ifstream input(SPORTS_CARS);
    if (input.is_open())
    {
      while (!input.eof())
      {
        getline(input, car);
        input >> price;
        input.ignore();
        listOfCars.push_back(car);
        listOfPrices.push_back(price);
      }
    input.close();
  }
  }
};

class Cart
{
public:
  Cart() {};
  ~Cart() {};
  void select()
  {
    InventoryItem a;
    for (int i = 0; i < a.listOfCars.size(); i++)
    {
      cout << a.listOfCars[i] << endl;
    }
  }
};

int main() {
  InventoryItem item;
  item.readSports();
  Cart cart;
  cart.select();
}
cooldog
  • 1
  • 1
  • On a side note: [`while (!input.eof())`](https://stackoverflow.com/questions/5605125/) is wrong. `while (!input.eof()) { getline(input, car); ... }` should be `while (getline(input, car)) { ... }` Also, why are you maintaining separate `vector`s for the cars and prices? Why are you not using a single `vector` that hold a `struct`/`class` with the relevant details of each car? – Remy Lebeau Apr 25 '22 at 23:48
  • I made a more reproducible example of my problem. – cooldog Apr 26 '22 at 00:10

1 Answers1

1

The problem is that you create an empty 'Inventory' and then try to read it

InventoryItem a;  <<<<==== empty
string carName;
cout << "What car do you want: ";
getline(cin >> ws, carName);
for (int i = 0; i < a.listOfCars.size(); i++) <<<====

You need to somehow have an 'loaded' Inventory and pass it as a parameter to this function. Or have the Cart constructor take an Inventory as an argument.

Its hard to propose an exact solution without seeing the whole code base.

OK now that you have made a good minmal example - heres how to fix it

  void select(InventoryItem &inv)
  {
    for (int i = 0; i < inv.listOfCars.size(); i++)
    {
      cout << inv.listOfCars[i] << endl;
    }
  }

and in main

cart.select(item);

Maybe this is just an English language thing , but calling the inventory 'InventoryItem' is odd. 'InventoryItem' suggests one thing thats in the inventory. I would expect to see an 'Inventory' class that contains 'InvertoryItem's - of maybe just 'Car' objects

pm100
  • 48,078
  • 23
  • 82
  • 145
  • What other code would you need I can add more. – cooldog Apr 25 '22 at 23:47
  • @cooldog Well, for starters, the code that is creating the inventory and cart, and then calling `select()` to populate them. How do you expect people to help you without a [mcve]? – Remy Lebeau Apr 25 '22 at 23:50
  • @cooldog this is a fundamental question about the design of your app, the relationship between classes, where data gets loaded etc. I cannot say which bits need to be shown, really all of it, but thats probably a lot – pm100 Apr 25 '22 at 23:52