C++ 从向量数组循环中获取数据,以便以后使用C++;
你好,我写了这段代码。。它询问您有多少不同类型的项,然后根据这些项生成向量数组。。然后询问每种类型的商品数量,然后询问价格,然后计算价格*商品。。因此,我需要从该循环中获取price*项目,以供以后使用。以下是我的代码:C++ 从向量数组循环中获取数据,以便以后使用C++;,c++,arrays,C++,Arrays,你好,我写了这段代码。。它询问您有多少不同类型的项,然后根据这些项生成向量数组。。然后询问每种类型的商品数量,然后询问价格,然后计算价格*商品。。因此,我需要从该循环中获取price*项目,以供以后使用。以下是我的代码: float products() { cout << "How many diffrent products ? "; int nProducts; //Takes number of diffrent type products c
float products()
{
cout << "How many diffrent products ? ";
int nProducts;
//Takes number of diffrent type products
cin >> nProducts;
//This is for the vector array it takes nProducts as parameter of how many items it has
vector<float> fCountPrice(nProducts);
//This is just for to have diffrent number in each cout
int x = 1;
//this loop adds value for all items in array
for (int i = 0; i < fCountPrice.size(); i++ & x++)
{
cout <<"How many of product "<< x << " you have? ";
cin >> fCountPrize[i];
//This asks for price of each product so it can be multiplied
cout << "Price of product ? ";
float fPrice;
cin >> fPrice;
fCountPrice[i] = fPrice * fCountPrize[i];
cout << fCountPrize[i] << endl;
}
}
float产品()
{
cout>n产品;
//这是针对向量数组的,它将nProducts作为它有多少项的参数
矢量fCountPrice(非产品);
//这只是为了在每一列中有不同的数字
int x=1;
//此循环为数组中的所有项添加值
对于(int i=0;in产品;
//这是针对向量数组的,它将nProducts作为它有多少项的参数
矢量fCountPrice(非产品);
矢量fCountPrize(非产品);
//这只是为了在每一列中有不同的数字
int x=1;
//此循环为数组中的所有项添加值
对于(int i=0;i 你能返回一个单独的float
。也许你想返回它们的向量并返回fCountPrice
。差不多了。。现在我在'retVal=products()中找不到与'operator='匹配的“非常适合我。你有没有包括相关的标题,特别是#includei,我已经在我所有的文件中包括了和……我可能还需要什么吗?@Pau Lee我已经更新了完整的程序,准确地复制它并告诉我你想要什么get@PauLee是否应该有两个不同的向量,fCountPrice
和fCountPrize
不清楚您的原始代码,它使用了两个向量,但只声明了一个
#include<iostream>
#include<vector>
using namespace std;
vector<float> products() //change return type
{
cout << "How many diffrent products ? ";
int nProducts;
//Takes number of diffrent type products
cin >> nProducts;
//This is for the vector array it takes nProducts as parameter of how many items it has
vector<float> fCountPrice(nProducts);
vector<float> fCountPrize(nProducts);
//This is just for to have diffrent number in each cout
int x = 1;
//this loop adds value for all items in array
for (int i = 0; i < fCountPrice.size(); i++, x++)
{
cout <<"How many of product "<< x << " you have? ";
cin >> fCountPrize[i];
//This asks for price of each product so it can be multiplied
cout << "Price of product ? ";
float fPrice;
cin >> fPrice;
fCountPrice[i] = fPrice * fCountPrize[i];
cout << fCountPrize[i] << endl;
}
return fCountPrice;
}
int main(){
vector<float> retVal;
retVal=products();
cout<<retVal[0];
}