Tylv Tylv - 4 months ago 12
C++ Question

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:

C3861 'main': identifier not found.

any ideas where I went wrong or how I can properly call the main function?

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

Answer

You can't call main yourself. 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.