程序挂起较小的整数

时间:2012-11-07 02:21:47

标签: c

我遇到下面粘贴的代码问题,它在运行时只挂起。 VS2010没有给我任何警告或错误。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


void clear_buffer(void)
{
    while(getchar() != '\n');
}

int validate(int low, int high) {
    int num;

    scanf("%d", &num);
    while(num < low || num > high) 
    {
        clear_buffer();
        printf("INVALID! Must enter value between %d and %d: ", low, high);
        scanf("%d", &num);
    }
    return num;
}

int getRand(int max) {
    int number;
    number = rand() % max + 1;
    return number;
}

int validatePick(int pick, int one, int two, int three, int four, int five) {
    int valid = 0;

    if (pick != one && pick != two && pick != three && pick != four && pick != five) {
        valid = 1;
    } else {
        valid = 0;
    }
    return valid;
}
void prnt(int qty, int one, int two, int three, int four, int five, int six) {
    int i = 0, flag = 0;
    while (flag != 2) {
        flag = 0;
        if (sort2(&one, &two) == 0 && sort2(&three, &four) == 0 && sort2(&five, &six) == 0)
            flag = 1;
        if (sort2(&two, &three) == 0 && sort2(&four, &five) == 0)
            flag = 1;
        flag += flag;
    }

    printf("Picks: %d, ", one);
    while (i <= qty){
        if (i== 2 && qty == 2)
            printf("%d\n", two);
        else if (i == 2 && qty != 2) 
            printf("%d, ", two);

        if (i == 3 && qty == 3) 
            printf("%d\n", three);
        else if (i == 3 && qty != 3) 
            printf("%d, ", three);

        if (i == 4 && qty == 4)
            printf("%d\n", four);
        else if (i == 4  && qty != 4)
            printf("%d, ", four);

        if (i == 5 && qty == 5)
            printf("%d\n", five);
        else if (i == 5  && qty != 5) 
            printf("%d, ", five);

        if (i == 6 && qty == 6) 
            printf("%d\n", six);
        i++;
    }
}



int sort2(int *n1, int *n2) {
    int tmp, valid = 0;

    if (*n1 > *n2)
    {
        tmp = *n2;
        *n2 = *n1;
        *n1 = tmp;
        valid = 1;
    }
    return valid;
}

int main () {
    int num1, num2;
    int pick, one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;

    srand(time(NULL));

    printf("LOTTERY GENERATOR\n");
    printf("Enter the maximum value between 1 and 100: ");
    num1 = validate(2,100);
    printf("Enter quantity of numbers to pick, between 1 and 6: ");
    num2 = validate(1, 6);

    one = getRand(num1);
    while (two == 0 || three == 0 || four == 0 || five == 0 || six == 0) {
        pick = getRand(num1);
        if (validatePick(pick, one, two, three, four, five) == 1 && two == 0)
            two = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && three == 0)
            three = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && four == 0)
            four = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && five == 0)
            five = pick;
        else  if (validatePick(pick, one, two, three, four, five) == 1)
            six = pick;
    }
    prnt(num2, one, two, three, four, five, six);
}

如果我为Enter the maximum value between 1 and 100输入3,然后2,程序就会挂起。我不明白为什么会这样。我没有在代码中看到错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这可能是由于缓冲造成的。请注意,如果输入3,则检查通过,并且不会调用clear_buffer。尝试更改函数,以便在输入后调用它。

这就是为什么我不喜欢scanf这么多 - 很容易忘记缓冲区的状态,并发现你的程序陷入循环。我喜欢阅读整行,比如使用fgets,然后将其标记并自己解析。对于像这样的简单程序,使用strtol甚至atoi非常简单。至少在这种方式下,我对所有输入的边缘情况都有更直接的了解。