如果声明无法工作/正在达成

时间:2015-02-10 16:16:49

标签: c user-interface if-statement int case

我目前有一个程序进行输入,最初将b的值设置为TRUE(即b = 1)。然后switch语句启动,将c的值设置为TRUE(即c = 1)。用户的下一个输入将b的值设置为FALSE,但由于某些原因从未达到第一个if语句,因为行"mvprintw(22,24,"It has reached it");"永远不会打印在我的屏幕上,尽管事实上b的值是假(b = 1),现在c的值为真(c = 1)。

我尝试过使用嵌套的if而不是案例,但这会使事情进一步复杂化,坦率地说不起作用。关于这个问题的任何意见将非常感谢!

int moveC(int y, int x, int b, int i)
    { //first input from user, b is True, so first case occurs
      //second input from user, b is false, so second case occurs, however, the if first if statement is never reached, but the second one is
        int c = FALSE;

        switch(b)
        {
             case TRUE:
                 c = TRUE; //this part is first reached from initall user input
                 refresh();
                 mvprintw(26,26,"value of c is... %d",c);
                 break;

             case FALSE:
                 if(c == 1) //this part is never reached, even though the second user input is (b = 0 i.e false, and c = 1, i.e true)
                 {
                      mvprintw(22,24,"It has reached it");
                      mvprintw(y,x+7,"^");
                      refresh();
                      break;
                 }

                 else if(c == 0) //this if statement is always used even if c is not zero
                 {
                    mvprintw(y,x,"^");
                    refresh();
                    break;
                 }

1 个答案:

答案 0 :(得分:2)

你声明的moveC()中的

int c = FALSE;

这使得它成为一个驻留在堆栈中的自动变量,因此每次调用都会c创建并再次使用FALSE进行初始化,c == 1中的条件case TRUE可以永远不会成真。如果您希望在moveC()的第二次通话中获得您在第一次通话中指定的值,则必须声明它

static int c = FALSE;