对零售销售计划的建议

时间:2015-10-12 01:38:06

标签: c++

我需要帮助设置int ProductNumber[]int ProductQuantitydouble RetailValue[]。我不确定如何初始化这些。

  

在线零售商销售五种产品,其零售价格如下:   产品1,$ 2.98
  产品2,4.50美元
  产品3,​​$ 3.98
  产品4,4.49美元   产品5,$ 6.87

#include "RetailSales.h"
#include <iostream>
using namespace std;

RetailSales::RetailSales()
{

}

void RetailSales::enterProducts()
{
int productNumber[] =
int ProductQuantity[] =
double RetailValue[] =

}

void RetailSales::displayTotalValue()
{

}

标头文件

/*
* CSIS1600
* A04a Retail Sales
* class RetailSales header file
*/

#ifndef RETAILSALES_H
#define RETAILSALES_H

class RetailSales
{
public:
RetailSales();
void enterProducts();
void displayTotalValue();

private:
double productPrice[6];
double productTotal[6];
int productQuantity[6];
double totalRetailValue;
};

#endif // RETAILSALES_H

如何设置我的数组以包含上面的数字?

1 个答案:

答案 0 :(得分:0)

使用std::vector

对于您直接了解其尺寸和元素的容器(如产品价格),您可以轻松构建vector,如下所示:

const std::vector<float> retailValue = { 2.98f, 4.5f, 3.98f, 4.49f, 6.87f };

retailValue现在包含5个值,无法修改。

对于您不知道要开始的值的容器(例如,您需要用户输入),您可以从空vector开始,并在知道值后填充它:

// Empty container to store values
std::vector<int> productNumbers;
// For each product price, get a corresponding product number
for (auto n = 0; n < retailValue.size(); ++n)
{
    auto productNumber = 0;
    // Get a value from the console
    std::cin >> productNumber;
    // don't forget to handle errors from std::cin
    ...
    // Add this product number to the container
    productNumbers.push_back(productNumber)
}

如果您无法使用std::vector

你可以用一个简单的数组做同样的事情,关键的区别是你不能有一个空的数组。您需要知道开始时需要多少元素,而std::vector可以动态增长

const int numberOfProducts = 5;
// Array of numbers that can't change.
const float retailValue[] = { 2.98f, 4.5f, 3.98f, 4.49f, 6.87f };
int productNumbers[numberOfProducts] = {};
// For each product that has a price, obtain a product number from the console
for (auto n = 0; n < numberOfProducts; ++n)
{
    auto productNumber = 0;
    // Get a value from the console
    std::cin >> productNumber;
    // don't forget to handle errors from std::cin
    ...
    // Add this product number to the container
    productNumbers[n] = productNumber;
}
相关问题