C++ 零售计划的建议
我需要有关设置C++ 零售计划的建议,c++,C++,我需要有关设置int-ProductNumber[]、int-ProductQuantity和double-RetailValue[]的帮助。我不确定应该如何初始化这些 在线零售商销售五种产品,其零售价格如下: 产品1,2.98美元 产品2,4.50美元 产品3,3.98美元 产品4,$4.49 产品5,6.87美元 如何设置数组以包含上述数字?使用! 对于一个容器,您可以直接知道它的大小和元素(如产品价格),很容易构建向量,如下所示: const std::vector<float>
int-ProductNumber[]
、int-ProductQuantity
和double-RetailValue[]
的帮助。我不确定应该如何初始化这些
在线零售商销售五种产品,其零售价格如下:
产品1,2.98美元产品2,4.50美元
产品3,3.98美元
产品4,$4.49
产品5,6.87美元 如何设置数组以包含上述数字?使用! 对于一个容器,您可以直接知道它的大小和元素(如产品价格),很容易构建
向量,如下所示:
const std::vector<float> retailValue = { 2.98f, 4.5f, 3.98f, 4.49f, 6.87f };
如果无法使用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;
}
const int numberOfProducts=5;
//无法更改的数字数组。
常量浮点零售值[]={2.98f,4.5f,3.98f,4.49f,6.87f};
int productNumbers[numberOfProducts]={};
//对于每个有价格的产品,从控制台获取产品编号
用于(自动n=0;n>产品编号;
//不要忘记处理来自std::cin的错误
...
//将此产品编号添加到容器中
productNumber[n]=productNumber;
}
您似乎只需要初始化上述数组中的值的帮助。对吗?如果你只是问这个问题,就有必要澄清你的问题(而不是放弃你的整个作业文本,因为它似乎与问题无关)。我必须(希望)改进你的要求是的。很抱歉没有提前提及,我只能使用数组,不能使用向量进行此分配:(谢谢,这很有帮助。
const std::vector<float> retailValue = { 2.98f, 4.5f, 3.98f, 4.49f, 6.87f };
// 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)
}
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;
}