所以我到处寻找,但找不到任何可以帮助我解决问题的东西。
这就是我的主界面的样子(我删除了菜单的 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);
}
我还在其中添加了一个函数,它最初来 self 的函数头文件,但当它不起作用时,我认为当我将它放入 main 中时它会起作用。但它仍然不起作用。
关于打印结构,这是我的函数头文件中的函数,我还添加了结构和原始的添加乘积函数。
#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()
不会修改由 const&
传递的提供的 vector
:
void displayProducts(const vector<Product>& vect)
参见range-for C++11 中引入了更简单的范围迭代代码。
关于c++ - 如何打印结构 vector ?是不是没有保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20356622/