0

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;
}}}
4
  • 4
    Please don't use goto. Please also simplify your code to the simplest possible example that demonstrates your issue. Commented Sep 11, 2013 at 23:40
  • 2
    You should be careful not to give such a huge wall of code; you should find the smallest amount of code that replicates the problem at hand. S.O. won't take arbitrary code and just 'find the problem'. Also, formatting of code matters. Please do it. Commented Sep 11, 2013 at 23:40
  • 1
    1) Indentation is your friend 2) == is the equality operator, while = does assignment. Commented Sep 11, 2013 at 23:42
  • I'm not totally sure what you mean by "tried a if loop"...? Commented Sep 11, 2013 at 23:44

3 Answers 3

2
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.

Sign up to request clarification or add additional context in comments.

2 Comments

wow thank you I didnt realize I did that. Well that fixed the issue but in the future what would you use for this program instead of a goto
@user2770643 look at my answer about functions.
1
...

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.

Comments

0

use the "break" keywork

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.