c++ - for循环中的数组加法

标签 c++ arrays for-loop addition

最后一个 for 循环有问题。想要数组值的总和。

#include<iostream>
using namespace std;

// Arrays

int main()
{
    const int numEmploye = 6;
    int horas[numEmploye];
    int total = 0;

    for (int i = 0; i < numEmploye; i++) {
        cout << "entre las horas trabajadas por el empleado " << i + 1 << ": ";
        cin >> horas[i];
    }
    for (int i = 0; i < numEmploye; i++) {
        cout << horas[i] << endl;
    }

    for (int i = 0; i < numEmploye; i++){
        cout << total += numEmploye[i];
    }

    system("Pause");
    return 0;
}

最佳答案

您正在输出 += 运算符的返回值。您还为数组使用了错误的变量。

for (int i = 0; i < numEmploye; i++){
    total += horas[i];
}
cout << total << endl; // endl flushes the buffer

关于c++ - for循环中的数组加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092881/

相关文章:

c++ - 将 std::shared_ptr 添加到 map 会破坏程序行为

c++ - 链接 boost 库

php - 如何将数组键从大写更改为小写?

arrays - for 循环中的协议(protocol)一致性检查?

c++ - "Attempting to reference a deleted function"

c++ - 这个回文划分算法的时间复杂度是多少?

Javascript - 从数组中删除唯一元素

php - 删除某些值时如何排列键?

java - 使用扫描仪读取文本,存储在数组中并使用每个循环输出大写Java

java - 获取单词的第一个字母并将其放在最后