陷入无限循环

时间:2014-05-08 12:03:08

标签: c++

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

如果收银员开始新订单并希望关闭订单,则会输入T。但是,当尝试退出订单并循环回while(moreCustomers)的开头时,什么也没发生。我试图通过设置while(moreItems)来退出moreItems = false;循环,但在执行此操作后,它会卡在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 to\n 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;之后输入的所有命令都能正常工作。

1 个答案:

答案 0 :(得分:1)

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

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

基本上,断点会导致程序停在该行。还可以尝试在此行上设置断点:

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

运行程序,按一个键,等待断点被击中。然后使用&#34; Step Over&#34; “调试”菜单上的选项。这会逐行运行您的程序,每次按下&#34; Step Over&#34; (F10这样做也快得多)。继续步进以查看代码中执行的执行路径。您也可以将鼠标悬停在变量上以查看其值。

Theres在网上加载有关使用visual studio进行调试的信息,但是如果你掌握了上述内容,那么你将会很好地完成