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

时间:2016-08-05 11:02:08

标签: c++ function return call

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

我正在尝试回调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;
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

回到main

                else {
                    quiz.close();
                    ;
                }

答案 2 :(得分:1)

您无法自己致电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放在要结束函数/程序的位置。