gpt4 book ai didi

c++ - 使用数组的总工资 (C++)

转载 作者:行者123 更新时间:2023-11-28 02:16:49 25 4
gpt4 key购买 nike

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

#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 是单元化的,您使用它的方式会导致未定义的行为。我认为你的意思是将 to 初始化为 0。

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

  • for 循环声明中的第一个术语是错误的。我认为您打算在那里声明 index = 0。如果不是,则应将其留空。

  • 开头附近计算 Gross[index] 的那一行是错误和多余的。

关于c++ - 使用数组的总工资 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com