C ++中的退出功能无法正常工作

时间:2015-09-24 06:50:39

标签: c++

因此,我尝试制作一项功能,让我在完成游戏后退出游戏,或者您可以选择再次游戏。重播选项有效,但每当我尝试退出部分时,它就会回到游戏的开始。

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

1 个答案:

答案 0 :(得分:1)

您在ifs中使用赋值运算符而不是equals运算符。

更改

if (replay = 1) {

代表

if (replay == 1) {

在同样问题的其他地方也这样做。