简体   繁体   English

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

[英]How do I close my program correctly in C++?

I am writing one of my first programs which reads from a file and allows you to play a game, I have been told the exit function is not a good idea. 我正在编写我的第一个程序,该程序从文件读取并允许您玩游戏,但有人告诉我退出功能不是一个好主意。

I am trying to call back to main in order to close the program correctly but I get the following error: 我试图回叫main以便正确关闭程序,但是出现以下错误:

C3861 'main': identifier not found. C3861'main':找不到标识符。

any ideas where I went wrong or how I can properly call the main function? 有什么想法我出错了或者如何正确调用main函数?

Code Below: 下面的代码:

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

Instead of calling main, you can simply return from the extra function. 除了调用main,您还可以简单地从extra函数返回。 The program then continues execution from where you called extra . 然后,程序将从您调用extra地方继续执行。

Just return to main . 刚回到main

                else {
                    quiz.close();
                    𝙧𝙚𝙩𝙪𝙧𝙣;
                }

You can't call main yourself. 您不能自己称呼main When you call a function and it gets to the end, the function pointer/flow will return to the calling code. 当您调用函数并到达最后时,函数指针/流将返回到调用代码。

Let's consider the general structure of your code: 让我们考虑一下代码的一般结构:

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

Note I have simply put return where you want to end the function/program. 注意,我只是将return放在要结束函数/程序的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何“正确地”在 VSCODE 的 C++ 程序中包含 Python.h header? - How do I "Correctly" include a Python.h header in my C++ program in VSCODE? C ++:如何检查我的窗口是否即将关闭? - C++: How do I check if my window is about to close? 如何将 libcurl 链接到 linux 中的 c++ 程序? - How do I link libcurl to my c++ program in linux? 如何在 C++ 程序中使用下标数字? - How do I use subscript digits in my C++ program? 当我关闭程序并在C ++中再次运行它时,如何停止记录覆盖 - How can I stop my records from overwriting when i close the program and run it again in c++ 如何在我的程序中添加背景声音,直到我在 c++ 中关闭控制台才停止 - how to add a background sound in my program that does not stop until I close the console in c++ 我如何编程当前程序以将命令输入到C ++中的另一个控制台中? - How do i program my current program to input a command into another console in C++? 如果进程正在运行,我该如何关闭我的程序? - How do i close my program if a process is running? 如何使用基本菜单在 c++ 程序中修复我的计算器答案? - How do I go about fixing my calculator answers in my c++ program with a basic menu? 如何获得 Win32 C/C++ 程序的命令行选项? - How do I get the command line option for my Win32 C/C++ program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM