0

code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
#include "Skirts.h"
#include "Tops.h"
#include "Bottoms.h"
#include "Cart.h"
using namespace std;

void showMenu()
{
    cout << "WELCOME TO THE CLOTHING STORE!\n------------------------------\n\n";
    cout << "1. Shop for a top!" << endl;
    cout << "2. Shop for a pair of pants or shorts!" << endl;
    cout << "3. Shop for a skirt!" << endl;
    cout << "4. View your cart!" << endl;
    cout << "5. Quit the application!" << endl;
    cout << "Enter your option: ";
}

void input(Clothing &cl, Cart &c, int option)
{
    Tops t;
    Bottoms b;
    Skirts s;
    int option2;

    cl.ask();
    cout << "\nDo you want to add this item to your cart?";
    cout << "\n1. Yes\n2. No\n";
    cout << "Enter your option: ";
    cin >> option2;

    switch(option2)
    {
        case 1 :
            if(option == 1)
            {
                cl.print();
                c.addTop(t);
            }
            else if (option == 2)
            {
                cl.print();
                c.addBottom(b);
            }
            else if (option == 3)
            {
                cl.print();
                c.addSkirt(s);
            }
            cout << endl << endl;
            break;
        case 2: cout << "Okay!" << endl;
    }
}

int main()
{
    Tops t;
    Bottoms b;
    Skirts s;
    Cart c;
    int option;

    while (true)
    {
        showMenu();
        cin >> option;
        switch (option)
        {
            case 1: input(t, c, 1); break;
            case 2: input(b, c, 2); break;
            case 3: input(s, c, 3); break;
            case 4: cout << c << endl; break;
            case 5: return 0; break;
            default:  cout << endl;
        }
        cout << endl;
    }

    return 0;
}

The ask function asks the user to input things about each object (Tops, Bottoms, Skirts; derived from class Clothing). I'm trying to save this object into a vector of that object that is a private member of the class Cart. The three vectors are

vector<Tops> tops;
vector<Bottoms> bottoms;
vector<Skirts> skirts; 

Each of them have their own function to add an object into the vector. However, when I add something, and then try to cout the cart (I overloaded the << operator for Clothing, Cart, Tops, Skirts, and Bottoms), the changes to the vector is not shown. It's almost as if it's adding the default object, as in every variable (string) being blank.

If I ask the user to input the variables in an object, and then I add the object to its corresponding vector, and then cout the cart, all in the main function, it works. However, it isn't working in this context, and I do not know why.

I think it's because I'm asking questions about an object Clothing &c, and then I add this new object Tops t, Bottoms b, or Skirt s into the vector, that has nothing to do with it. It's almost as if I need to set the object &c equal to t, b, or s. Maybe by overloading the = operator? I don't know...

Help please! :(

  • You appear to be printing *before* you add the items, is that intentional? Otherwise you will probably have to show us what class `Clothing` does. – Galik Dec 04 '17 at 06:44
  • the print function just says that they are about to add that item in the cart, it doesn't print the vector Tops, Bottoms, or Skirts. – Emily Lerman Dec 04 '17 at 06:46
  • I fear without a bit of code regarding your factory, base class `Clothing`, at least one class inherited from `Clothing` and your manager `Cart` it's impossible to tell why it doesn't work. But it reminds me a little bit to a problem which I once had, see https://stackoverflow.com/q/16896847/2328447 But of course it can have a gazillion other causes :/ – user2328447 Dec 04 '17 at 10:04

0 Answers0