C ++:Program" Not Responding"执行时

时间:2018-05-26 20:04:41

标签: c++ loops debugging while-loop do-while

我正在处理这个C​​ ++家庭作业,当我尝试执行时,.exe文件保持"没有响应。"我关闭了程序和Codeblocks。当我尝试打开另一个程序时,我知道重新启动我的任务并再次尝试,Codeblocks停止工作。现在,当我尝试打开它时,它会一直说'#34;没有回应。"

程序应该为用户掷2个骰子,用户决定是否要保留骰子数量,并且一旦用户完成他们的最终掷骰,它应该比较用户的最终滚动总计到计算机的总数 - 谁赢得更高。

我的程序没有返回任何错误...但是在尝试执行时它不起作用...现在我担心我有一个损坏的文件或使Codeblocks停止工作的东西。任何想法是什么以及我如何让Codeblocks再次正常运行?

以下是该计划:

#include <iostream>
#include <cstdio>
#include <time.h>
#include <stdlib.h>
using namespace std;

int main()
{
srand(time(0));

int Roll1, Roll2, UserRoll, Roll3, Roll4, ComputerRoll;
char Hold;
char PlayAgain;

    do
    {
      Roll1 = 1+(rand()%6);
      Roll2 = 1+(rand()%6);
      UserRoll = Roll1 + Roll2;

        do
        {
            cout << "Beat the computer!" << endl;
            cout << "" << endl;
            cout << "You rolled a " << Roll1 << " and a " << Roll2 << "(total= " << UserRoll << ")" << endl;
            cout << "" << endl;
            cout << "Do you want to keep those? (Y/N): " << endl;
            cin >> Hold;

            if (Hold == 'N')
            {
                cout << "Rolling again! " << endl;
            }
        } while (Hold == 'N');

            if (Hold == 'Y')
            {
                cout << "You chose to keep your numbers. Your total is: " << UserRoll << endl;
            } break;

    }  while (true);

    do
    {
      Roll3 = 1+(rand()%6);
      Roll4 = 1+(rand()%6);
      ComputerRoll = Roll3 + Roll4;

        do
        {
            cout << "" << endl;
            cout << "The computer rolled a " << Roll3 << " and a " << Roll4 << endl;
            cout << "" << endl;
            cout << "(total= " << ComputerRoll << ")" << endl;

                if (ComputerRoll < UserRoll)
                {
                cout << "Congratulations, you win! " << endl;
                }
                break;

                if (ComputerRoll > UserRoll)
                {
                cout << "Sorry, you lose. " << endl;
                }
                break;

                if (ComputerRoll == UserRoll)
                {
                cout << "You tied. " << endl;
                }
                break;
        } while (true);
    } while (true);
return 0;

}

2 个答案:

答案 0 :(得分:1)

查看你的循环。您在break语句之外使用if。因此,您可以在首次do...while之后立即中断if循环。

答案 1 :(得分:1)

它“没有响应”,因为你的程序是一个无限循环:

do
{
  do
  {
    ...
    break; //exit this inner loop
  } while(true);
  // will continue here 
} while(true); // no way to get out!