c++ - xCode 和 C++ 数组。这怎么可能?在 C++ 中声明一个没有大小的数组

标签 c++ c arrays xcode

所以我创建了一个空数组并为其前 4 个元素分配了一些值。我正在将相同的代码复制到 Visual Studio 中,但该程序无法运行,因为它无法理解该行。那么在 C++ 中如何声明空数组?

#include <iostream>
using namespace std;

double inTotal(double quartersQuantity, double dimesQuantity, double 
nickelsQuantity, double penniesQuantity) {


double total = 0.25 * quartersQuantity + 0.10 * dimesQuantity + 0.05 * 
nickelsQuantity + 0.01 * penniesQuantity;

return total;

}

int main(int argc, const char * argv[]) {

string coinNames[] = {"quarters", "dimes", "nickels", "pennies"};
double coinQuantity[] = {};
int counter = 0;

for (string values : coinNames) {
    cout << "How many " << values << " do you have\n";
    cin >> coinQuantity[counter];
    counter++;

}

double total = inTotal(coinQuantity[0], coinQuantity[1], 
coinQuantity[2], coinQuantity[3]);
cout << "Your dollar value is " << total << " dollars." << endl;



return 0;
}

enter image description here

最佳答案

你可以用这种方式初始化数组。

int coinQuantity[4];  // declare the array
for (int i = 0; i < 4; i++) // ...initialize it
{
    coinQuantity[i] = 0;
}

关于c++ - xCode 和 C++ 数组。这怎么可能?在 C++ 中声明一个没有大小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49663751/

相关文章:

c++ - 存储 NUL 字符 (ASCII 0)

c++ - 从 catch (...) C++ 获取异常对象地址

c++ - 在运行时在 C++ 中组合几个类的类

c 绘图函数无法正确绘图

c - 如何组合 2 个整数以获得 1?

c++ - 右值引用转换后的地址变化

arrays - swift : protocol extension and arrays

arrays - 矩阵作为应用仿函数,不是 Monad

arrays - 在 F# 中实例化数组?

c - 如何在 ncurses 中获得亮白色?