【问题标题】:How do I close my program correctly in C++?如何在 C++ 中正确关闭我的程序?
【发布时间】:2016-08-05 11:02:08
【问题描述】:

我正在编写我的第一个程序,它从文件中读取并允许您玩游戏,有人告诉我 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() 函数,再次加载问题等等。另外,当 extra() 函数完成时,它将继续执行 main() 函数,该函数将顺利退出并返回“0”; .
  • 请注意,main 中的if (UI = 1) extra(); 将始终调用extra()。测试应该是if (UI == 1)
  • @PeteBecker 这就是我的问题所在!
  • @Tylv 我的意思是不能低估侦探技能的重要性。 Any C 运算符引用说明了赋值和相等之间的区别。并不是说这是一个“愚蠢”的问题——不是,问这个问题是完全合理的。本质上,它在任何 C 教程/参考资料中都很容易发现,而且对于诸如此类的问题,SO 是相对无情的。您有责任进行研究,在这种情况下,这很容易做到。如果不是,我建议现在查找参考资源,以便以后出现类似问题时参考。祝你好运!

标签: c++ function return call


【解决方案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 放在您要结束函数/程序的位置。

【讨论】:

  • 感谢您发现 for 循环错误,我已修复它,但由于某种原因,只有返回函数 return; 不起作用。
  • “不起作用”是什么意思?您的代码是否实际输入了else 部分?我怀疑我们现在已经转移到另一个问题了。
  • 我希望用户能够选择退出游戏,如果他们的生命 = 0,当我使用返回时;函数你放置它的地方,它带我到额外的开始,但不回到主,所以我可以关闭程序
  • extra 似乎只是从main 调用一次实际上你是从extra 内部调用extra 所以它将返回到extra 的最后一次调用
  • 我现在可以正常工作了,非常感谢我在使用返回函数后添加了一些内容,导致错误让我相信它不起作用
【解决方案2】:

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

【讨论】:

  • 能否请您出示您所指内容的代码 sn-p?我相信我尝试过,但仍然不成功
【解决方案3】:

只需返回main

                else {
                    quiz.close();
                    ??????;
                }

【讨论】:

  • 当我使用它时,我的游戏仍然重复
  • @Tylv:不,执行返回到main,退出。你在做别的事情。可能您遇到了编译错误,因此您再次运行了旧的可执行文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-13
  • 2020-07-08
  • 1970-01-01
相关资源
最近更新 更多