0

I crated a program that is supposed to add objects to a vector and the print them, however when i add objects it only prints the last object added, i cant seem to find the error this is what i have:

#include <vector>
#include <iostream>
#include <string>

int main() {
  std::vector<GroceryItem*> item;
  GroceryItem* grocery = new GroceryItem;
  std::string option = " ";
  while((option != "x") && (option != "X")){
    std::cout << "Welcome to Kroger\n";
    std::cout << "A- add item\n";
    std::cout << "X - Exit\n";
    std::cout << "type option:";
    std::cin >> option;
    std::cin.ignore();
        if(option == "A" || option == "a") {
          std::cout << "Enter UPC, Product Brand, Product Name, and Price\n";
          std::string item_;
          double price_ = 0.0;
          std::getline(std::cin, item_);
          grocery->upcCode(item_);
          std::getline(std::cin, item_);
          grocery->brandName(item_);
          std::cin.ignore();
          std::getline(std::cin, item_);
          grocery->productName(item_);
          std::cin >> price_;
          grocery->price(price_);
          item.push_back(grocery);

        } else if(option == "x" || option == "X") {
          std::cout << "Here is an itemized list of the items in your shopping basket:\n";
          for (GroceryItem* gcry  : item) {
            std::cout << *gcry;
          }
         }
        }
      }

this is the overloaded extraction operator declared on .cpp

std::ostream& operator<<( std::ostream& stream, const GroceryItem& groceryItem ) {
  stream << "\"" << groceryItem.upcCode() << "\", " << "\"" << groceryItem.brandName() << ", "
   << groceryItem.productName() << ", " << groceryItem.price() << "\n";
   return stream;

this is a sample output:

Welcome to Kroger
A- add item
X - Exit
type option:a
Enter UPC, Product Brand, Product Name, and Price
2134567890
heinz
ketchup
222
Welcome to Kroger
A- add item
X - Exit
type option:a
Enter UPC, Product Brand, Product Name, and Price
2345678
coca cola
coke
3.33
Welcome to Kroger
A- add item
X - Exit
type option:x
Here is an itemized list of the items in your shopping basket:
"2345678", "coca cola, oke, 3.33
"2345678", "coca cola, oke, 3.33


Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
sadboy99
  • 39
  • 5
  • 1
    Did you try to step line by line through your code with a debugger? What is the line that doesn't behave as expected? – Thomas Sablik Feb 10 '20 at 00:18
  • 1
    There is most likely no reason to use `new` here. You can store `GroceryItem` directly in the vector (instead of a pointer to one). [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – walnut Feb 10 '20 at 00:25

2 Answers2

1

Move this statement

GroceryItem* grocery = new GroceryItem;

inside the loop.

For example

  std::string option = " ";
  while((option != "x") && (option != "X")){
    std::cout << "Welcome to Kroger\n";
    std::cout << "A- add item\n";
    std::cout << "X - Exit\n";
    std::cout << "type option:";
    std::cin >> option;
    std::cin.ignore();
        if(option == "A" || option == "a") {
              GroceryItem* grocery = new GroceryItem;
              //...

And it seems the call of ignore in this code snippet

      std::getline(std::cin, item_);
      grocery->brandName(item_);
      std::cin.ignore();             //  <===
      std::getline(std::cin, item_);

should be removed.

Vlad from Moscow
  • 224,104
  • 15
  • 141
  • 268
1

your grocery variable is a pointer, which means it is pointing to an object, when you change the pointer's variable, both the pointer and the object in the vector get changed.

Like Vlad suggested, try moving it into the loop, don't forget to use delete if you're using new

flyingCode
  • 102
  • 2
  • 10