[英]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.