我目前有一个继续获取输入的程序,该程序最初将b的值设置为TRUE(即b = 1)。然后switch语句开始执行,将c的值设置为TRUE(即c = 1)。用户的下一个输入将b的值设置为FALSE,但是由于某些原因从不到达第一个if语句,因为行"mvprintw(22,24,"It has reached it");"从未在屏幕上打印,尽管b的值为false (b = 1),并且c的值现在为true(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;
                 }

最佳答案

moveC()中,您声明

int c = FALSE;


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

static int c = FALSE;

关于c - 如果声明无法解决/无法解决,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28436472/

10-09 17:19