get data out of vector array loop for later use C++

时间:2022-10-06 04:17:37

Hello so i wrote this code.. It asks how many diffrent type of items you have, then makes vector array based on that.. Then ask's how many items of each type you have, then asks price and it counts price * items.. So i need to get the price * items out from that loop for later use here's my code:

你好,所以我写了这个代码..它询问你有多少不同类型的项目,然后根据它制作矢量数组..然后询问你有多少项目,然后询问价格和它计算价格*项目..所以我需要从该循环中获取价格*项目以供以后使用,这是我的代码:

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

Now i got to figure out how to get data from fCountPrize to return it to the program.. This is just small part of the program. I need to get data from the fCountPrize array to use in the main() function.. I don't have any idea how to do that. I tried declaring it in header but when i try to cout it in main get error: invalid types 'float[int]' for array subscript

现在我必须弄清楚如何从fCountPrize获取数据以将其返回到程序中。这只是程序的一小部分。我需要从fCountPrize数组中获取数据以在main()函数中使用..我不知道如何做到这一点。我尝试在标题中声明它,但当我尝试在主要获取错误中cout它:无效类型'float [int]'为数组下标

1 个解决方案

#1


1  

#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];


}

#1


1  

#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];


}