c++中的退出特性不起作用

Quit feature in C++ not working

本文关键字:不起作用 退出 c++      更新时间:2023-10-16

所以我尝试着创造一个能够让玩家在完成游戏后退出游戏的功能,或者让玩家能够选择再次体验游戏。重放选项工作,但每当我尝试退出部分,它只是回到游戏的开始。

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

在if语句中使用赋值运算符而不是等号运算符

改变
if (replay = 1) {

if (replay == 1) {

对于同样的问题在其他地方做同样的事情