如何在 C++ 中正确关闭我的程序?

How do I close my program correctly in C++?

我正在编写我的第一个程序,它从文件中读取并允许您玩游戏,我被告知 exit 函数不是一个好主意。

我正在尝试回调 main 以正确关闭程序,但出现以下错误:

C3861 'main': 未找到标识符。

我哪里出错了或者我如何正确调用主函数有什么想法吗?

代码如下:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;
void extra() {
    int lives = 3;
    int UI, intAnswer;
    int opt = 0;
    string IN, NoQ, q, c1, c2, c3, Answer;
    fstream quiz;
    cout << "Welcome to the guessing game!" << endl;
    quiz.open("QuizQuestions.txt");
    getline(quiz, IN);
    cout << "There are " << IN << " Questions" << endl;
    while (quiz.good() && opt !=2) {
        getline(quiz, q);
        cout << "Question " << q << endl;
        getline(quiz, c1);
        cout << c1 << endl;
        getline(quiz, c2);
        cout << c2 << endl;
        getline(quiz, c3);
        cout << c3 << endl;
        getline(quiz, Answer);
        intAnswer = stoi(Answer);
        cout << "What answer do you think it is? ";
        cin >> UI;
        if (UI == intAnswer) {
            lives++;
            cout << "You got it right! You now have " << lives << " lives left " << endl << endl;
            //i = 0;
        }
        else {
            cout << "You got the answer wrong sorry, the correct answer is " << Answer << endl;
            lives--;
            cout << "You now have " << lives << " lives" << endl;
            //i = 0;
            if (lives < 1) {
                cout << "You lose, would you like to play again? 1 for yes, 2 for no? ";
                cin >> opt;
                if (opt = 1) {
                    cout << endl;
                    extra();
                }
                else if (opt = 2) {
                    quiz.close();
                    return;
                }
            }
        }
    }
    quiz.close();
}


int main() {
int UI;
cout << "Would you like to do the quiz? 1 - yes other - no ";
cin >> UI;
if (UI = 1) {
    extra();
}
return 0;
}

您可以从 extra 函数简单地 return 而不是调用 main。然后程序从您调用 extra.

的地方继续执行

只是 return 到 main

                else {
                    quiz.close();
                    ;
                }

您不能给自己打电话 main。 当你调用一个函数并且它结束时,函数 pointer/flow 将 return 到调用代码。

让我们考虑一下您的代码的一般结构:

void extra() {
    for (int i = 0; i = 1; i++) {
                  //^---I suspect you don't mean this, maybe i<1, or 3, or...
                  // recall == and -= are different
            //snipped some details
            if (UI == intAnswer) {
                lives++;
                cout << "You got it right! You now have " << lives << " lives left " << endl << endl;
                i = 0;
            }
            else {
                cout << "You got the answer wrong sorry, the correct answer is " << Answer << endl;
                lives--;
                cout << "You now have " << lives << " lives" << endl;
                i = 0;
                if (lives < 1) {
                    cout << "You lose, would you like to play again? 1 for yes, 2 for no? ";
                    cin >> UI;
                    if (UI = 1) {
                        cout << endl;
                        extra();
                      //^--- I suspect you don't need this recursive call
                    }
                    else {
                        quiz.close();
                        return;
                     // ^---- return back to where we started   
                    }
                }
            }
        }
    }
    quiz.close();
    system("pause");
}


int main() {
  int UI;
  cout << "Would you like to do the quiz? 1 - yes other - no ";
  cin >> UI;
  if (UI = 1) {
      extra();//we come back here after the function stops
  }
  return 0;
}

请注意,我只是将 return 放在您想要结束 function/program 的位置。