程序运行然后说它已停止工作。调试问题

时间:2011-10-15 01:46:35

标签: c

我正在使用代码块在C中编写代码,这些代码块会按计划进行,并告诉您是否对某个项目进行了双重预订。

文件本身将设置为:

2
2
0 1 5
1 8 12

其中第一个数字是有多少个时间表,第二个数字是该时间表中有多少个已安排的项目。

接下来的几行将0视为星期日,将1视为星期一等等,然后是当天的时间表,即开始时间到结束时间。

我的代码是:

//10/14/11
//This Code functions to let the user know if schedule conflicts exist in a certain amount of schedules.

#include <stdio.h>

int main() {
    int Hours_week[168];
    int Num_schedules;
    int Num_items;
    int i;
    int j;
    int k;
    int w;
    FILE* ifp;
    int day;
    int start;
    int end;
    int Schedule;
    int booked;
    int index;

      //initialize the array by setting each spot equal to zero.
    for (w=0; w<168; w++){
        Hours_week[w] = 0;
    }

    //read in the file schedule.txt
    ifp = fopen("schedule.txt","r");

    //Read in the number of schedules
    fscanf(ifp, "%d", &Num_schedules);

    //create a loop to run as many times as there are schedules.
    for (i=0; i<Num_schedules; i++){

        //read in the number of items in schedule.
        fscanf(ifp, "%d", &Num_items);

        j = 0;
        while (j < Num_items){
            j++;

            //read in the day, start time, and end time.
            fscanf(ifp, "%d", day);
            fscanf(ifp, "%d", start);
            fscanf(ifp, "%d", end);

            //Multiply day by 24 and then add start and end hours to get their new times.
            start = (day * 24) + start;
            end = (day * 24) + end;

            for (k = start; k <= end; k++){
                if (Hours_week[k] == 0)
                    Hours_week[k] = 1;
                else if (Hours_week[k] == 1)
                    Schedule = booked;
            }

            //close the file
            fclose(ifp);


        if (Schedule == booked)
            printf("I'm sorry, schedule %d has a conflict in it.\n", i);
        else
            printf("Great Planning! You have no schedule conflicts in schedule %d.\n", i);
        }
    }
return 0;
}

2 个答案:

答案 0 :(得分:5)

你错过了这里的运营商地址

         //read in the day, start time, and end time.
        fscanf(ifp, "%d", day);
        fscanf(ifp, "%d", start);
        fscanf(ifp, "%d", end);

正确应该是

         //read in the day, start time, and end time.
        fscanf(ifp, "%d", &day);
        fscanf(ifp, "%d", &start);
        fscanf(ifp, "%d", &end);

奇怪,上面的说法是正确的!是时候睡觉了,对我来说至少!

答案 1 :(得分:0)

因为这看起来很像家庭作业,我将提供关于问题的提示,而不是接近修复的任何事情 - 学习的很大一部分是打架斗争,但一次被困几个小时没什么好玩的。

int Schedule;
int booked;
/* ... */
            else if (Hours_week[k] == 1)
                Schedule = booked;
/* ... */
    if (Schedule == booked)
        printf("I'm sorry, schedule %d has a conflict in it.\n", i);

您已使用booked而未先将其初始化。 (任何合理的编译器都应该警告 - 请不要忽略编译器警告。并非所有警告都需要静音,但编译器的作者非常好程序员,当事情看起来不正确时会发出警告。)

        //read in the day, start time, and end time.
        fscanf(ifp, "%d", day);
        fscanf(ifp, "%d", start);
        fscanf(ifp, "%d", end);

您已将daystartend的(可能未初始化)值作为地址传递给fscanf(3)。同样,这是您的编译器应该警告您的情况。不要忽视警告。 :)

    j = 0;
    while (j < Num_items){
        j++;

这段代码已经成熟,需要用for循环替换。不要只是使用不同的循环类型。 while循环罚款,如果您在循环中使用j做任何远程有趣的事情,这可能没问题。但你只是递增它。坚持使用无聊的旧习语,这将使阅读代码变得更加容易。 (它伴随着练习。)没有什么错误这个,只是我想知道为什么你选择一个不同的循环结构,而不是所有其他循环看起来基本相同。