-1

I just got into c++ and I'm just experimenting. I want to make a simple program which takes users input and calls one of 2 functions, then the function will print a line and ask the user if they want to go again. The issue is c++, for some reason, does not allow me to call main by simple saying main(); Is there any way to call the main function from another function? I am looking for the simplest solution there is, but I can't find anything :/

Here's the code:

#include <iostream>
#include <string>
using namespace std;


int do_math() {
    cout << "Math" << endl;

    string user;
    cout << "would you like to go again? (y or n): " << endl;
    cin >> user;

    if (user == "y") {
        main();
    }

    else if (user == "n") {
        cout << "Okay, bye!";
        exit(0);
    }
    return 0;
}

int do_eng(){
    cout << "Eng";

    string user;
    cout << "would you like to go again? (y or n): " << endl;
    cin >> user;

    if (user == "y") {
        main();
    }

    else if (user == "n") {
        cout << "Okay, bye!";
        exit(0);
    }
    return 0;
}
int main() {

    string user;

    cout << "Would you like to do math or end?:";
    cin >> user;

    if (user == "math") {
        do_math();
    }
    else if (user == "end") {
        do_eng();
    }
    return 0;
}
SzczureX
  • 91
  • 1
  • 1
  • 5

2 Answers2

0

The issue is c++, for some reason, does not allow me to call main by simple saying main(); Is there any way to call the main function from another function? I am looking for the simplest solution there is, but I can't find anything :/

No, you don't want to call main from any of your code.

Not that you should want to do this ... but the simplest solution is to provide a callable function, and get into it in the simplest way from main.

Perhaps:

int myMain()
{
  string user;

  cout << "Would you like to do math or end?:";
  cin >> user;

  if (user == "math") {
      do_math();
  }
  else if (user == "end") {
      do_eng();
  }
  return 0;
}


int main(int, const char**) 
{
   return myMain();
}

Lesson 1 - try to add another level of indirection (i.e. myMain()) does not have the restrictions of main()

Lesson 2 - learn something about recursion ... it seems you probably want to avoid it, here. (i.e. if you always invoke myMain(), how does your program ever terminate?

Lesson 3 - On my system, if the program terminates, I can up-arrow and launch it trivially. Terminal shells do this stuff for you. Perhaps this would be a better approach ... to always terminate unless the user selects one of the action choices (math, burp, etc.)

Lesson 4 - research other programs and how their user interface works. Find a model you like.


Note - I suppose, for your code to call myMain() again, you will need to 'forward declare' the function.

2785528
  • 5,438
  • 2
  • 18
  • 20
-1

No, the standard specifically disallows calling main() from program code. What you want is to have a loop in main :

int main()
{
    bool bContinue;
    do
    {
        /* do something */
        std::cout << "Do you want to go again?";
        cin >> bContinue;
    } while(bContinue);
}
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23