从向量数组循环中获取数据以供以后使用
get data out of vector array loop for later use C++
你好,所以我写了这段代码。它询问你有多少种不同类型的项目,然后基于此创建向量数组。然后问你每种类型有多少件物品,然后问价格,然后计算价格*物品…因此,我需要从循环中取出price * items以供以后使用,下面是我的代码:
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;
}
}
现在我要弄清楚如何从fCountPrize获取数据以将其返回给程序。这只是这个项目的一小部分。我需要从fCountPrize数组中获取数据,以便在main()函数中使用。我不知道该怎么做。我试图在标题中声明它,但当我试图在主get错误:无效类型'float[int]'为数组下标
#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];
}
相关文章:
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 如何在 C++ 中获取中继器的委托数据?
- 从 Rcpp Catch 测试中获取有关"inst/extdata"的数据
- C++ - 忽略并从其他文件获取数据
- 如何获取C++字符数据类型的地址
- 从存储为 Windows 资源 (c++) 的 png 中获取 png 文件数据
- 很好的语法来获取对向量/数组数据的大小引用?
- 如何从网站获取数据并将其传输到数据库?
- 获取文件数据预处理器宏
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- 如何在新的派生对象中获取基本对象的数据?
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 如何使用CAPL的诊断功能获取CAN传输的数据(256字节)?
- 获取 R 数据帧的内存地址
- 当 c++ 需要一种数据类型并获取另一种数据类型时会发生什么?
- 从更改日志获取最新的 USN 数据
- 套接字:在没有memcpy的情况下,使用recvfrom将UDP数据获取到字对齐的缓冲区
- 如何将控制台输出数据获取到命令提示符下
- 将零mq消息数据获取到std::vector<char>
- 从指定坐标[x, y]处的位图数据获取像素颜色