陷入无限循环

Getting stuck in an infinite loop

本文关键字:无限循环      更新时间:2023-10-16

我的程序运行得很顺利,然后在注释它并添加一些最后的修饰之后,它停止了对我的工作。我遇到问题的函数是使用其他地方定义的几个对象/函数,所以我只是想知道是否有人可以确认我的逻辑是正确的,并且无限循环不是语法错误的产物。谢谢你的时间,这是我遇到的问题:

如果收银员开始一个新订单并想要关闭他的订单,则输入T。然而,当试图退出订单并循环回while(moreCustomers)的开始时,什么都没有发生。我试图通过设置moreItems = false;来退出while(moreItems)循环,但这样做之后,它会卡在while(moreItems)循环中,不会回到while(moreCustomers)。语法是否有意义,我是否可以通过设置moreItems = false;来打破循环?

bool moreCustomers = true;      
while (moreCustomers)
{ 
    // get input to start new order or close register
    drawInstruct("Enter N to start a new order or E ton close the register.");
    char* setFmt = "@"; // the input must be a letter
    char input[7];      // char array that stores input from cashier
    s.GetStr(xLeftCoord + 1, yTopCoord + 1, input, 1, setFmt, true);   
    for(int x = 1; x < 10; x++) // clear the input field
    {  
        s.ClearScreenPos(x, 1);
    }        
    if (input[0] == 'N') // if a new order is requested
    {   
        bool moreItems = true;  
        while (moreItems)
        {                  
            getInput(input);        
            if(input[1]) // if input is not a single char
            {
                if (input[0] == 'M') // get the desired number of multiples for the current item and update the tape and display area accordingly
                {                       
                    custTape.handleMultiples(atoi(input)); // adds multiples to tape
                    curVal = isUPC->price * (atoi(input)); // updates the current item price                            
                    drawDisplayArea(curVal);               // updates the display area
                }
                else // invalid number of multiples, prompt for new multiple
                {
                    drawInstruct("Invalid command. Please try again.");
                    s.Delay();
                }                    
            }
            else if (input[0] == 'T') // close the order
            { 
                 drawInstruct("Order cancelled.");
                 s.Delay();                                                         
                 moreItems = false; // customer order is complete, exit loop                    
            }
            else // invalid command, get new input from the cashier
            {
                drawInstruct("Invalid command. Please try again.");
                s.Delay();
            }
        }
    }
    else if (input[0] == 'E') // close the register
    {               
        moreCustomers = false; // no more customers, exit the program
    }
    else // invalid command, get new input from the cashier
    {
        drawInstruct("Invalid Command. Please try again.");
        s.Delay();
    }
} 

我不能退出else if(input[0] == 'T'), moreItems = false;之后我输入的任何命令都能正常工作

我设置了一个断点在第一个moreItems = false;看看它是否被击中。我猜不是。你已经用Visual Studio标记了这个问题,所以如果这是你正在使用的,请参阅如何设置断点的链接:

http://msdn.microsoft.com/en-us/library/vstudio/k80ex6de%28v=vs.100%29.aspx

基本上,断点使程序在该行停止。还可以尝试在这一行设置一个断点:

if (input[0] == 'N')

运行程序,按下一个键,等待到达断点。然后使用Debug菜单上的"Step Over"选项。这将逐行运行您的程序,每次您按"Step Over"(F10也这样做,更快)。继续执行以查看代码中的执行路径。您还可以将鼠标悬停在变量上以查看它们的值。

网上有很多关于visual studio调试的内容,但是如果你掌握了上面的内容,你就会很顺利了