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

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

本文关键字:关闭程序 C++      更新时间:2023-10-16

我正在编写我的第一个程序,它可以从文件中读取并允许您玩游戏,有人告诉我退出函数不是一个好主意。

为了正确关闭程序,我正试图回叫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;
}

您不能自己调用main。当您调用一个函数并且它到达末尾时,函数指针/流将返回到调用代码。

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

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放在您想要结束函数/程序的地方。

您可以简单地从extra函数返回,而不是调用main。然后,程序从您调用extra的位置继续执行。

返回main即可。

                else {
                    quiz.close();
                                ;
                }