user3062299
user3062299

Reputation: 55

How Do I Print Out a Vector of Structs? Is It Not Saving the Data?

So I've looked everywhere, and I can't find anything to help me with my problem.

This is what my main looks like (I took out the switch statement for my menu to save room).

 #include <iostream>
 #include <iomanip>
 #include <vector>
 #include "functions.h"
 using namespace std;

 void addAProduct (vector<Product>);

 int main()
 {
        cout << setprecision(2) << fixed << showpoint << left;                          //Format Output

    char   option;                                              //Declare Variable

    vector<Product> productVect;                                    //Create Empty Vector of Structs

    bool menu = true;

    while (menu)                                                //While Loop
    {
        cout << "\nMenu";                                       //Menu
        cout << "\n1. Display Products";
        cout << "\n2. Add a Product";
        cout << "\n3. Edit a Product";
        cout << "\n4. Exit";
        cout << "\n\n";
        cin >> option;

void addAProduct (vector<Product> vect)
{
    Product tempProduct;

    tempProduct.upc = 100000 + (rand() % 99999);
    cin.ignore(256,'\n');
    cout <<"\nPlease enter a name for the Product's Manufacturer. ";
    getline(cin, tempProduct.manufacturer);
    cout << "\nPlease enter a name for the Product. ";
    getline(cin, tempProduct.name);
    cout << "\nPlease enter a value for the Product's Price. ";
    cin >> tempProduct.price;
    cout << "\nPlease enter a value for the Product's Quantity. ";
    cin >> tempProduct.quantity;
    tempProduct.value = tempProduct.quantity * tempProduct.price;
    vect.push_back(tempProduct);
}

I also added a function into this, it was originally from my functions header file but it when it wasn't working, I thought it would work when I put it into main. But it still isn't working.

And in regards to printing out the structure, here's my function from my function header file, I also added the struct and the original add product function.

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
using namespace std;

struct Product                                                  //Struct of Product
{
    int upc;                                                //All Members to Be Determined By User at a Later Time with the Exception of the UPC
    string manufacturer;
    string name;
    float price;
    int quantity;
    double value;
};

//Display Products
void displayProducts(vector<Product> vect)
{
    if(vect.empty())
        cout << "\nSorry, no values exist in the database.";
    else
        for (int count = 0; count < vect.size(); count++)                           //For Loop to Display All Products
            {
                cout << "\nUPC: " << vect[count].upc << "\nManufacturer: " << vect[count].manufacturer << "\nProduct Name: " << vect[count].name << "\nPrice: $" << vect[count].price << "\nQuantity: " << vect[count].quantity << "\nTotal Value: $" << vect[count].value << endl;
            }
        cout << endl;
}

//Add a Product
void addProduct(vector<Product> vect)
{
    Product tempProduct;

    tempProduct.upc = 100000 + (rand() % 99999);
    cin.ignore(256, '\n');
    cout << "\nPlease enter a name for the Product's manufacturer. ";
    getline(cin, tempProduct.manufacturer);
    cout << "\nPlease enter a name for the Product. ";
    getline(cin, tempProduct.name);
    cout << "\nPlease enter a value for the Product's Price. $";
    cin >> tempProduct.price;
    cout << "\nPlease enter a value for the Product's Quantity. ";
    cin >> tempProduct.quantity;
    tempProduct.value = tempProduct.quantity * tempProduct.price;
    vect.push_back(tempProduct);

}

I have no compiler errors, but when I do try to run it by adding a product and then displaying it, it still outputs that there are no values.

Any ideas?

Upvotes: 2

Views: 10617

Answers (1)

hmjd
hmjd

Reputation: 121971

Pass by reference:

void addAProduct (vector<Product>& vect)
                               //^

to avoid adding to a copy. The code as posted adds elements to a copy of the supplied argument.

As displayProducts() does not modify the supplied vector pass by const& instead:

void displayProducts(const vector<Product>& vect)

See range-for introduced in c++11 for simpler range iteration code.

Upvotes: 2

Related Questions