陷入无限循环
Getting stuck in an infinite loop
我的程序运行得很顺利,然后在注释它并添加一些最后的修饰之后,它停止了对我的工作。我遇到问题的函数是使用其他地方定义的几个对象/函数,所以我只是想知道是否有人可以确认我的逻辑是正确的,并且无限循环不是语法错误的产物。谢谢你的时间,这是我遇到的问题:
如果收银员开始一个新订单并想要关闭他的订单,则输入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调试的内容,但是如果你掌握了上面的内容,你就会很顺利了
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 超出 int 的值范围后的无限循环
- 为什么这段代码会导致无限循环?
- 运行无限循环的最小二叉树问题
- 在分配和发布递增循环迭代器时C++无限循环(gcc 错误?
- 连接线程时的无限循环
- 为什么这种方法会进入无限循环?
- 由于QTextStream的无限循环
- Qt 在另一个线程中无限循环
- 从文件 C++ 读取时的无限循环
- 试图找出为什么我的代码保持无限循环