C ++退出循环不起作用

时间:2013-09-11 23:37:52

标签: c++ loops

这是一个简单的项目,它可以完成所有工作,因为它应该减去它时不会退出。

我尝试了很多不同的方法,包括goto语句我尝试了一个if循环,但除了错误之外什么都没有。当前代码没有错误只是我不知道去哪里退出。它不一定是花哨的,这只是我的第二个程序

// C// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{

srand(static_cast<unsigned int>(time(0)));  //seed random number generator

int secretNumberE = rand() % 10 + 1; 
int secretNumberM = rand() % 100 + 1;// random number between 1 and 100
int secretNumberH = rand() % 1000 + 1;
int tries = 0;
int guess;
char play;


{ 
cout << "\tWelcome to Guess My Number\n\n";
START:
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n\n";


int choice;
cout << "Choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "You picked Easy.\n";
do
{
    cout << "Enter a guess 1-10: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberE)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberE)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberE);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 2:
cout << "You picked Normal.\n";
do
{
    cout << "Enter a guess 1-100: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberM)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberM)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberM);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;
case 3:
cout << "You picked Hard.\n";
do
{
    cout << "Enter a guess 1-10: ";
    cin >> guess;
    ++tries;

    if (guess > secretNumberH)
    {
        cout << "Too high!\n\n";
    }
    else if (guess < secretNumberH)
    {
        cout << "Too low!\n\n";
    }
    else
    {
        cout << "\nThat's it! You got it in " << tries << " guesses!\n";
    }

} while (guess != secretNumberH);
std::cout << "Do you want to play again y/n? ";
cin >> play;
if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}
break;

default:
cout << "You made an illegal choice.\n";
goto START;
return 0;
}}}

3 个答案:

答案 0 :(得分:2)

if ( play = 'y' ){
goto START;
}
else if (play = 'n') 
{
cout << " Thank you for playing. ";
return 0;
}

第一个if将永远为真,因为您使用的是赋值运算符而不是相等运算符。您需要使用==来比较两个值。

答案 1 :(得分:1)

...

char play;


{ 

...

您的代码中似乎有一个额外的{用于包装您的内容。

我清楚你需要做的事情,缩进。 IDE中可能有一个自动格式化快捷方式。开始使用它。这是不可能很好地阅读你的代码,从长远来看,这将导致一系列错误的简单事情,你将会敲打你的脑袋。

goto强烈建议不要使用此功能,除非您知道自己在做什么。

开始学习功能/方法,它们将挽救您的生命,并全面改善您的工作流程。

您可以用它替换goto

函数的例子..

int startGame(){
   cout << "Difficulty Levels\n\n";
   cout << "1 - Easy\n";
   cout << "2 - Normal\n";
   cout << "3 - Hard\n\n";


   int choice = 0;
   cout << "Choice: ";
   cin >> choice;
   return choice;
}

其他事情。

if ( play = 'y' )需要if ( play == 'y' )重复冲洗,因为您正在尝试检查相等(有条件),而不是设置=所做的值。

此外,始终初始值int choice;应为int choice = 0;或某个默认值。

答案 2 :(得分:0)

使用“break”关键字

do
{
    if(mycondition)
    {
        break;
    }
} while (loopCondition);