【问题标题】:Gross Pay using Arrays (C++)使用数组的总工资 (C++)
【发布时间】:2015-11-19 06:34:16
【问题描述】:

我正在编写一个代码来使用数组计算七名员工的总工资。这是我目前所拥有的

#include <iostream>
#include <iomanip>
using namespace std;

int main ()
{
//Set all constants and variables
const int SIZE = 7;             //Size of all arrays
int emID[SIZE] = {1234, 4563, 8765, 4568, 9867, 9235, 7684};

double  Hours[SIZE],
        Rate[SIZE],
        Gross[SIZE];

int index;

Gross[index] = (Hours[index] * Rate[index]);

//Explain Program
cout << "This program calculates an employees gross pay\n";

for (Hours[index];index <= 6; index++)
{
    cout << "How many hours did employee " << emID[index] << " work?\n";
    cin >> Hours[index];
}

for (Rate[index]; index <= 6; index++)
{
    cout << "Enter the pay rate for " << emID[index] << endl;
    cin >> Rate[index];
}

for (Gross[index]; index <=6 ; index++)
{
    cout << "The gross pay for " << emID[index] << " is " << Hours[index] * Rate[index];
}

}

不幸的是,程序在第一个“for”循环之后终止。有什么建议吗?

【问题讨论】:

  • index需要初始化。
  • thats not how this works. Thats not how any of this works. 请再次阅读 for 循环的用法。
  • 这是我的第一堂编程课...

标签: c++ arrays


【解决方案1】:

您的代码中似乎有一些错误,我将在下面指出。

  • index 已单化,您使用它的方式会导致未定义行为。我认为你的意思是初始化为 0。

  • 您应该在for 循环之间将index 的值重置为0。目前,您将在第一个 for 循环中进行迭代。之后,由于index 将变为&gt; 6,您的代码将不会执行另外两个for 循环。

  • for 循环声明中的第一项是错误的。我想你的意思是在那里声明index = 0。如果没有,您应该将其留空。

  • 靠近开始处计算Gross[index] 的那一行是错误且多余的。

【讨论】:

  • 也未正确初始化和使用总数组,此外循环索引不应用作数组下标。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多