如何在C++中正确关闭程序
How do I close my program correctly in C++?
我正在编写我的第一个程序,它可以从文件中读取并允许您玩游戏,有人告诉我退出函数不是一个好主意。
为了正确关闭程序,我正试图回叫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();
;
}
相关文章:
- 如何在 qt 中从另一个线程运行 qt并发时关闭程序
- 如何在程序崩溃时关闭程序,没有任何警告屏幕等.C++窗口
- QT-从QDialog(MainWindow满载之前)关闭程序
- Gtkmm 3/C++,使用按钮而不是窗口关闭程序"X"
- c ++ Win32 API GetMessage 关闭程序在线程内
- nofify_all()关闭程序时会崩溃
- 每项分辨率的自动关闭程序
- Windows上的C 在启动后立即关闭程序
- 在QT中关闭程序之前,请显示对话框
- 用x关闭程序时内存泄漏
- 在不关闭程序的情况下写入文件后,从文件中获取保存的数据
- Qt:关闭模式对话框关闭程序
- 如何在用户输入字符并按回车键后关闭程序
- 视觉C++ - 在我关闭程序之前,ofstream 不会输出到文件
- C 使用 Y/N 选项循环开关。选择 N 不会关闭程序
- C++关闭程序的方法
- 防止Ctrl-C关闭程序
- 使'Q'关闭程序
- 在我关闭程序之前,对象尚未被处理
- 如何在C++中正确关闭程序