Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
去年关闭。
我不知道为什么我的程序可以在在线GDB中运行,而不能在Code:Blocks中运行。它应该允许一个人输入他们的小时费率,然后输入他们在过去4周中工作了多少小时,将这些总计加在一起并将其返回给用户。应该使用以小时为参数的函数。在Code:Blocks中,在输入第一个小时数后,它将终止程序。这是代码:
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
去年关闭。
我不知道为什么我的程序可以在在线GDB中运行,而不能在Code:Blocks中运行。它应该允许一个人输入他们的小时费率,然后输入他们在过去4周中工作了多少小时,将这些总计加在一起并将其返回给用户。应该使用以小时为参数的函数。在Code:Blocks中,在输入第一个小时数后,它将终止程序。这是代码:
#include <iostream>
#include <iomanip>
using namespace std;
// function declaration
float getGross(float hs[], int n);
int main()
{
float hours[4], sum;
sum = getGross(hours, 4);
cout << "Gross pay: $ " << setprecision(2) << fixed << sum << endl;
return 0;
}
// function definition
float getGross(float hs[], int n)
{
float wage, ot, total;
cout << "Please enter your hourly wage: " << endl;
cin >> wage;
cout << "Enter hours worked in each of the past four weeks (hit enter after each entry): " << endl;
// Storing 4 number entered by user in an array
for (int i = 0; i < n; ++i)
{
// Holding the array of hours entered
cin >> hs[i];
int j;
float weekPay[4];
if(hs[i] > 40)
{
ot = (hs[i] - 40) * 1.5;
weekPay[j] = (wage * 40) + (ot * wage);
total += weekPay[j];
}
else
{
weekPay[j] = (wage * hs[i]);
total += weekPay[j];
}
}
return total;
}
最佳答案
第40行的变量int j
未初始化,这会导致运行时错误。
试图清理代码:
// function definition
float getGross(float hs[], int n)
{
float wage, ot, total = 0;
cout << "Please enter your hourly wage: " << endl;
cin >> wage;
cout << "Enter hours worked in each of the past four weeks (hit enter after each entry): " << endl;
// Storing 4 number entered by user in an array
for (int i = 0; i < n; ++i)
{
// Holding the array of hours entered
cin >> hs[i];
if (hs[i] > 40)
{
ot = (hs[i] - 40) * 1.5;
total += (wage * 40) + (ot * wage);
}
else
{
total += (wage * hs[i]);
}
}
return total;
}
10-07 21:43