gpt4 book ai didi

c++ - 如何打印出结构 vector ?它不保存数据吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:47:21 25 4
gpt4 key购买 nike

所以我四处寻找,但找不到任何可以帮助我解决问题的东西。

这就是我的 main 的样子(我为我的菜单取出了 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 时它会起作用。但它仍然无法正常工作。

关于打印出结构,这是我的函数头文件中的函数,我还添加了结构和原始的 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() 不会修改由 const& 传递的提供的 vector:

void displayProducts(const vector<Product>& vect)

参见 range-for在 c++11 中引入,用于简化范围迭代代码。

关于c++ - 如何打印出结构 vector ?它不保存数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20356622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com