gpt4 book ai didi

c++ - 为什么进程返回-1?

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

运行以下代码:

int main()
{
double hour[3];
double charge[3];

double sum_hour = 0;
double sum_charge = 0;

for (int i = 1; i <= 3; i++)
{
cout<<"Enter the hours for car No. "<<i<<": ";
cin>>hour [i];

if (hour [i] <= 3.0)
{charge [i] = 2.00;}
if (hour [i] > 3.0 && hour [i] < 24)
{charge [i] = 2.00 + (ceil(charge [i] -3))*0.5;}
if (hour [i] == 24.0)
{charge [i] = 10.00;}

sum_hour = sum_hour + hour [i];
sum_charge = sum_charge + charge [i];
}

cout<<"Car"<<setw(10)<<"Hours"<<setw(10)<<"Charge"<<endl;


}

在执行了for 循环 并且代码没有运行cout 循环后:

Process returned -1 (0xFFFFFFFF) execution time...

最佳答案

在循环中for (int i = 1; i <= 3; i++)数组索引应该从0开始.将循环更改为:

for (int i = 0; i < 3; i++)

i is 3 时,您的数组索引超出范围这里cin>>hour [i]; , 它的未定义行为。

这里要加上,不要做浮点比较:

if (hour [i] <= 3.0)// 

尽管这与您的原始问题无关,但请阅读 Why doesn't my floating-point comparison work?

关于c++ - 为什么进程返回-1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28066234/

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