【问题标题】:Switch-Case statement seems to freeze my loopsSwitch-Case 语句似乎冻结了我的循环
【发布时间】:2014-08-01 22:30:42
【问题描述】:

这是我正在处理的程序中的几个循环。该程序似乎在printf("TEST2"); 之后停止推进。一切一目了然。我有什么遗漏吗?

我希望循环在设置开关语句中的值后重复。我知道它至少经历过一次。

#include "stdafx.h"
#include <stdlib.h>
#include <time.h>

char *names[] = { "Denise", "Inja", "Jane", "Karen", "Maggie", "Margaret", "MJ", "Queen", "Sherri", NULL }; //ptr for names, 9 nurses
const char days[5][10] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
int randomNurse();
#define total_nurses 9 //number of nurses on staff
#define days_in_week 5 //number of work days in a week


int main() {

srand(time(NULL));
int day, pos, candidate, i, j;
int slackers[4] = { 1, 1, 1, 1 }; //array that holds the selections for who isn't working
char **name_ptr = names;
/*0 = Denise, 1 = Inja, 2 = Jane, 3 = Karen, 4 = Maggie, 5 = Margaret, 6 = MJ, 7 = Queen, 8 = Sherri*/
int avail_nurses[total_nurses] = { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; //holds the status of each nurse, 0 = unavailable, 1 = available

/*prints names */
int temp_counter = 1; //counter
while (*name_ptr) {
    printf("%i) %s\n", temp_counter, *name_ptr);
    name_ptr++;
    temp_counter++;
}

/*this assumes that no more than FOUR nurses will be away on any given week*/
printf("\nEnter numbers that correspond to the nurses who won't be available for the week.\nType up to four numbers, each separated by a space.\n");
printf("When you are done, press \"Enter\".\n");
printf("If less than four nurses will be on leave, type a \"0\" in place of a selection.\n");
printf("Example: 1 2 5 0\n\n\n");

/*week selection of unavailable nurses*/
    do {
        printf("Who won't be here?  ");
    } while (scanf("%i %i %i %i", &slackers[0], &slackers[1], &slackers[2], &slackers[3]) != 4);

/*checks the selections made, and sets the available nurses to the correct value, zero if they are slacking||vacationing*/
for (int n = 0; n < 4; n++) {
    int slacker = slackers[n];
    if (slacker >= 1 && slacker <= 9)
        avail_nurses[slacker] = -1;
}


/*-----WEEKLY_ASSIGNMENT-----*/
int pos_per_day[days_in_week] = { 5, 9, 9, 8, 5 }; //number of nurses needed each day

int selection[days_in_week][total_nurses]; //the selected nurses per day 

for (i = 0; i < days_in_week; i++) {
    for (j = 0; j < total_nurses; j++) {
        selection[i][j] = -1; //initialize to -1 which means no nurse is selected
    }
}

//fill all the days of week 
for (day = 0; day < days_in_week; day++) {
    for (pos = 0; pos < pos_per_day[day]; pos++) { //for every position needed that day
        do {
            candidate = randomNurse();
        } while (!avail_nurses[candidate]); //looks for available nurses (phrasing)

        avail_nurses[candidate] = 0;  //change nurses status to not available
        selection[day][pos] = candidate;  //fill the output array with appropriate nurse
    }
    for (i = 0; i < total_nurses; i++) {
        avail_nurses[i] = 1; //initialize the nurses status for next day use
    }
    for (int n = 0; n < 4; n++) { //make sure we shame the slackers...
        int slacker = slackers[n];
        if (slacker >= 1 && slacker <= 9)
            avail_nurses[slacker] = -1;
    }
}

/*-----PRINTS SCHEDULE FOR WEEK-----*/
for (i = 0; i < days_in_week; i++) {
    printf("%-10s: ", days[i]);
    for (j = 0; j < total_nurses; j++) {
        if (selection[i][j] != -1)
            printf("%-10s ", names[selection[i][j]]);
    }
    printf("\n");
}
return 0;
}

/*function to generate random nurse*/
int randomNurse() {

return rand() % 9; //random number 0-8, to pick nurse
}

【问题讨论】:

  • 首先使用break 而不是continue。还有,AVAIL_NURSES的定义在哪里???请添加相关的代码。顺便说一句,看起来您可以简单地检查 slackers[n] 是否在 1 和 9 之间,然后使用 AVAIL_NURSES[slackers[n]-1] = 0 而不是整个 switch 语句。
  • 唯一可能陷入无限循环的代码是do-while 块。在该块中添加printf 以查看是否是问题所在。
  • 该死的R萨胡!!! UI 正要这么说!
  • 为什么,哦,为什么,你使用for-switch 构造?只需将整个 switch 语句替换为 int slacker = slackers[n]; if (slacker &gt;= 1 &amp;&amp; slacker &lt;= 9]) AVAIL_NURSES[slacker] = 0;
  • @barakmanos 我添加了所有代码。让我知道你的想法。

标签: c loops for-loop switch-statement


【解决方案1】:

您有未定义的行为。 pos_per_day 中的第二个值是 9,它超出了 select 数组的范围。从该数组中的每个值中减去一个可能足以修复它。

其他不好的问题:

  • 您需要验证scanf之后的输入数据。
  • switch 语句完全没有必要。用计算代替它。
  • 不要对变量使用大写。按照惯例,这仅适用于已定义的常量。
  • 不要硬编码 5 和 9 之类的数字。用 DEFINED CONSTANTS 替换它们。

必须学习如何使用您可用的调试器来调试这样的简单程序。

【讨论】:

  • 我不确定你所说的越界是什么意思。如果我在 for-loop 中省略了 switch-case 语句,该程序就会运行。更改数组的大小似乎没有帮助。我修复了所有其他问题并更新了代码。我希望得到进一步的批评。
  • 代码现在不同了,但现在 slacker 可以是 9 并且avail_nurses[9] 超出了范围。这是一个简单的调试,在这个站点上并没有提出好的问题。你真的需要学会自己做这件事。
  • 我不明白你所说的越界是什么意思。我知道我需要自己学习,但我应该如何学习我已经不明白的东西?
  • 编写代码、阅读代码、做教程、搜索网络、阅读书籍、实验、思考,我们学习事物的常用方式。大约需要 10 年才能真正擅长这些东西。没有捷径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
相关资源
最近更新 更多