问题描述:

well its a simple project it does everything as it should minus it wont exit when it is suppose to.

I have tried many different ways including goto statements I tried a if loop but got nothing but errors. The current code gives no errors just I dont know where to go to make it exit. It doesnt have to be fancy this is only my second program

// 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;

}}}

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

The first if will always be true, because you are using the assignment operator rather than the equality operator. You need to use == to compare two values.

网友答案:
...

char play;


{ 

...

There seems to be an extra { in your code that you are wrapping your stuff with.

Things that are clear to me that you need to work on, indentation. There is probably an auto-format shortcut in your IDE. Start using that. It's impossible to read your code well this will lead to a bunch of errors in the long run for simple things that you will be banging your head over.

goto highly recommend not using this unless you know what your doing.

Start learning functions/methods they will save your life and overall improve you workflow.

and you can replace goto with it.

example of a function..

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;
}

Other things.

if ( play = 'y' ) needs to be if ( play == 'y' ) rinse repeat this because you are trying to check for equality(conditional) not setting a value which is what one = does.

Also, ALWAYS INITIALIZE VALUES int choice; should be int choice = 0; or some default value.

网友答案:

use the "break" keywork

do
{
    if(mycondition)
    {
        break;
    }
} while (loopCondition);
相关阅读:
Top