所以我到处都看了,找不到任何可以帮助我解决问题的东西。

这就是我的主要外观(我为菜单节省了switch语句以节省空间)。

 #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);
}

我还向其中添加了一个函数,它最初来自我的函数头文件,但是当它不起作用时,我认为当我将其放入main时它将起作用。但它仍然无法正常工作。

关于打印结构,这是我的函数头文件中的函数,我还添加了struct和原始的add product函数。
#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);

}

我没有编译器错误,但是当我尝试通过添加产品然后显示它来运行它时,它仍然输出没有值。

有任何想法吗?

最佳答案

通过引用传递:

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

避免添加到副本。发布的代码会将元素添加到所提供参数的副本中。

由于displayProducts()不会通过vector修改提供的const&传递:
void displayProducts(const vector<Product>& vect)

有关更简单的范围迭代代码,请参见c++ 11中引入的range-for

10-07 16:27