阅读数据文件和帮助纠正程序

时间:2015-09-22 17:38:39

标签: c

我得到了一个计算任务,当火箭开始下降并附加到任务是一个名为rocket1.txt的文件。这是我第一次做一个程序,我们需要从另一个文件中读取信息。这就是我到目前为止所拥有的,如果有人能帮助我纠正它那么伟大。

    #include<stdio.h>
    #define FILENAME "rocket1.txt"
    int main(void)
    {
        /*Declare variables*/
        errno_t err;
        int number_of_pts, k;
        double rocket_acc, new_alt = 0;
        double old_alt = 0.0, old_time, new_time, rocket_vel;
        FILE *rocket1;

        /*Open File*/
        err = fopen_s(&rocket1, "rocket1.txt", "r");
        if (err)
            printf("Problem opening file. \n");
        else
        {
            while ((number_of_pts > 0) && (new_alt >= old_alt));
            {
                old_alt = new_alt;
                old_time = new_time;
                fscanf(rocket1, "%lf%lf%lf%lf", &new_time, &new_alt, &rocket_acc, &rocket_vel);

            }
            if (new_alt < old_alt)
                printf("The rocket begins to fall. \n")
                printf("%4.2f seconds and %4.2f seconds", old_time, new_time);

            else
            {

                printf("There is no Decrease in Altitude. \n");
            }

        }

        getchar();
            return 0;
    }

我因非法使用if else语句而出错(这是我第一次使用这些语句,我不确定循环是否适用于此类问题。附件提供

   24
   1.0000000e+01   2.9265380e+03   5.0821200e+02   4.3231640e+01
   2.0000000e+01   1.0170240e+04   9.2798610e+02   4.0723180e+01
   3.0000000e+01   2.1486260e+04   1.1832420e+03   1.0328000e+01
   4.0000000e+01   3.3835080e+04   1.1882285e+03  -9.3307000e+00
   5.0000000e+01   4.5250830e+04   1.0899705e+03  -1.0320900e+01
   6.0000000e+01   5.5634490e+04   9.8935650e+02  -9.8019000e+00
   7.0000000e+01   6.5037960e+04   8.9134700e+02  -9.8000000e+00
   8.0000000e+01   7.3461430e+04   7.9334750e+02  -9.7999000e+00
   9.0000000e+01   8.0904910e+04   6.9534750e+02  -9.8001000e+00
   1.0000000e+02   8.7368380e+04   5.9734700e+02  -9.8000000e+00
   1.1000000e+02   9.2851850e+04   4.9934700e+02  -9.8000000e+00
   1.2000000e+02   9.7355320e+04   4.0134750e+02  -9.7999000e+00
   1.3000000e+02   1.0087880e+05   3.0334400e+02  -9.8008000e+00
   1.4000000e+02   1.0342220e+05   2.0534500e+02  -9.7990000e+00
   1.5000000e+02   1.0498570e+05   1.3402000e+02  -4.4660000e+00
   1.6000000e+02   1.0610260e+05   2.6304000e+02   3.0270000e+01
   1.7000000e+02   1.1024650e+05   6.7618500e+02   5.2359000e+01
   1.8000000e+02   1.1962630e+05   1.2929950e+03   7.1003000e+01
   1.9000000e+02   1.3610640e+05   2.1234700e+03   9.5092000e+01
   2.0000000e+02   1.6209570e+05   3.1700000e+03   1.1421400e+02
   2.1000000e+02   1.9950640e+05   3.8340050e+03   1.8587000e+01
   2.2000000e+02   2.3877580e+05   3.8779450e+03  -9.7990000e+00
   2.3000000e+02   2.7706530e+05   3.7799500e+03  -9.8000000e+00
   2.4000000e+02   3.1437480e+05   3.6819500e+03  -9.8000000e+00

1 个答案:

答案 0 :(得分:1)

您在下面的代码段中遗漏了大括号和分号。

 if (new_alt < old_alt)
 { /* HERE*/
        printf("The rocket begins to fall. \n") ; /* HERE*/
        printf("%4.2f seconds and %4.2f seconds", old_time, new_time);
 } /* HERE*/
 else
 {
       printf("There is no Decrease in Altitude. \n");
 }