编辑:第一次,我问了太多问题。现在我没有提供足够的细节。简单地取出数组无济于事,因为我试图对数组进行算术运算并且它会误解数据。它可能有助于找出问题所在,因此我将其标记为正确答案并发表新文章。我不知道该怎么办,很明显我无法将全局传达给读者。
我需要一个用户定义的字符串,以便程序将其解释为数组。我试图实施来自user463035818的建议,但我认为建议不正确。
我要讨论的数组在程序此部分的顶部初始化为arrnStoreInput01A(已裁剪,因此更易于阅读)。我指示用户以“ i,j,k”的格式输入三个数字(如果更好,我将更改为“ i j k”-我已经测试了这两个数字)。现在的情况是,当我要求程序向我吐出结果(从底部开始的第4行)时,它是否显示十六进制而不是{i,j,k}(是否使用逗号)(结果是不同的)十六进制)。
发生了什么事?我该如何解决这个问题,以便我有一个1x3的整数数组,可以对3 * arrnStoreInput01A进行算术运算?谢谢。
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <limits>
#include <tuple>
int main()
{
int nNumCup = 0, nNumLem = 0, nNumSug = 0, nNumIce = 0;
float fCoH = 20.00, fCostCup25 = 1.99, fCostCup50 = 2.49, fCostCup100 = 2.99;
int arrnStoreInput01A[3];
std::cout << "Go to Cups \n \n";
std::cout << "Cups are availible in packs of 25, 50 and 100. \n"
"Please enter three numbers in \"i,j,k\" format for the \n"
"respective amounts of each of the following three products \n"
"you want to buy: \n \n"
"A) 25-pack of cups for " << fCostCup25 << "\n"
"B) 50-pack of cups for " << fCostCup50 << "\n"
"C) 100-pack of cups for " << fCostCup100 << "\n \n"
"For example, type \"0,4,0\" to purchase 4 packages of 50 cups or \n"
"type \"3,2,1\" to buy 3 packages of 25 cups, 2 packages of 50 cups \n"
"and 1 package of 100 cups. \n \n";
std::cin >> arrnStoreInput01A[0] >> arrnStoreInput01A[1] >> arrnStoreInput01A[2];
float arrnCostCup[3] = { fCostCup25,fCostCup50,fCostCup100 };
int arrnQuantCup[3] = { 25,50,100 };
std::cout << arrnStoreInput01A;
return 0;
}
答案 0 :(得分:3)
变量arrnStoreInput01A
是一个数组。没有operator<<
重载可用于打印数组。
相反,正在发生的事情是arrnStoreInput01A
衰减为它的第一个元素的 pointer ,并打印了该指针。
所以当你这样做
std::cout << arrnStoreInput01A;
真正发生的事情是
std::cout << &arrnStoreInput01A[0];
如果要打印数组的所有元素,则必须显式地对其进行迭代,以打印每个元素。
例如
for (auto const value : arrnStoreInput01A)
{
std::cout << value << ' ';
}