getch在一段时间内被跳过了

时间:2017-12-14 12:20:24

标签: while-loop putchar

我有这个功能,同时检查需要在1-6之间的正确输入但是当我调用此功能时它只是跳过所有getch和所有putchar。我做错了什么?

    int firstNum = 0;
    int secondNum = 0;
    int thirdNum = 0;
    int fourthNum = 0;
    int counter = 0;
    int counterMiss = 0;
    int counterInPlace = 0;
    int condition = 1;
    while(firstNum<0||firstNum>6||secondNum<0||firstNum>6||secondNum<0||secondNum>6||thirdNum<0||thirdNum>6||fourthNum<0||fourthNum>6) //loop that checks for input correct
    {
        if(counter>0)
        {
            printf("enter ONLY numbers beween 1-6\n"); //if there were any incorrect input it will trigger
        } 
        firstNum=getch();
        putchar(firstNum);
        secondNum=getch();
        putchar(secondNum);
        thirdNum=getch();
        putchar(thirdNum);
        fourthNum=getch();
        putchar(fourthNum);
        firstNum-=48;
        secondNum-=48;
        thirdNum-=48;
        fourthNum-=48;
        counter++;
}

1 个答案:

答案 0 :(得分:0)

如果需要许多相同的变量,请使用数组,而不是单独的标量变量。像这样的单独变量看起来很难看,不会缩放。

此外,您将变量初始化为零,然后测试它们是否小于零或大于六。它们不是,所以你的循环不会运行。