为什么过程返回-1?

问题描述:

运行下面的代码:为什么过程返回-1?

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?