Alex Carr
Alex Carr

Reputation: 49

Quit feature in C++ not working

So I'm trying to create a feature that will let me quit my game once you've completed it, or you have the option to play again. The replay option works, but whenever I try the quit part, it just goes back to the start of the game.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

using namespace std;

int main() {

SetConsoleTitle("Guess the number! | v1.0");

int replay;

replay = 1;

    int g, n;

play:

    g = NULL;
    srand(time(NULL));
    n = rand() % 20 + 1;
    system("cls");

    cout << "Number guessing game" << endl;
    cout << "********************" << endl;
    cout << endl;
    cout << "The random number has been generated. It is between 1 and 20" << endl;

    system("pause");

    while (g != n) {
        system("cls");
        cout << "Number Guessing Game" << endl;
        cout << "********************" << endl;
        cout << endl;
        cout << "Type a guess between 1 and 20 then press ENTER" << endl;
        cout << "Your guess: ";
        cin >> g;
    }
    if (g = 1113) {
        goto debugskip;
    }
debugskip:
    system("cls");
    cout << "You have found the number!" << endl;
    cout << "The number was " << n << endl;
    cout << "Would you like to play again? 1 for yes, 2 for no.";
    cin >> replay;
    if (replay = 1) {
        goto play;
    }
    else if (replay = 2) {
        exit(1);
    }
    return 0;
}

Upvotes: 0

Views: 61

Answers (1)

Anonymous Coward
Anonymous Coward

Reputation: 3200

You are using assignment operator instead of equals operator in your ifs.

Change

if (replay = 1) {

for

if (replay == 1) {

And do the same in the other places with the same problem.

Upvotes: 1

Related Questions