C 代码在GDB Online中起作用,但在代码:块中不起作用
C++ code works in GDB online but not in Code:Blocks
我无法弄清楚为什么我的程序在GDB Online中起作用,但在代码:块中不起作用。它应该允许一个人输入其小时率,然后输入他们在过去4周内工作了多少小时,将这些总数添加在一起,然后将总计退还给用户。它应该使用花费时间作为参数的函数。在代码中:阻止它在输入第一个小时数之后终止程序。这是代码:
#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;
}
变量int j
,在第40行中并不活化,这会导致运行时错误。
试图清理代码:
// 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;
}
相关文章:
- 为什么这段代码不起作用,我该如何解决?
- 为什么这些完全相似的代码不起作用?
- 我不明白为什么我的代码不起作用并且需要更长的时间来运行
- 我正在尝试解决一个需要数组总和值但代码不起作用的问题,我想做这样的事情
- 注释一行使代码工作,而没有它,代码不起作用
- 我的C++合并排序代码不起作用。我在这里错过了什么?
- C ++函数重新定义(代码不起作用 - 逻辑错误)
- 试图找到一个数字的平方根,但代码不起作用。C++
- 字符串代码不起作用
- 我正在努力在随机数组中查找最小值,有人知道为什么我的代码不起作用?C++
- 从 C++ 转换的 C# 代码不起作用
- 有人可以解释我,为什么我的代码不起作用吗?
- 当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好
- 无法在控制面板中创建轨道栏.示例代码不起作用
- CUDA我的共享内存代码不起作用,我缺少什么
- 为什么 C++ 中的 shell 脚本代码不起作用?
- 为什么C++代码不起作用(strncpy_s)?
- 模板函数声明为 void - 代码不起作用?
- 无法弄清楚为什么这个Arduino代码不起作用?
- "check if member exists using enable_if"中修改的代码不起作用