you should not call main from anywhere nor recursively. Some compilers figure it out OK, but it is not ensured to work. I once had a compiler that tolerated double main and could compute a value for me using the return statement :) But this stuff is hackery. main is type int, and main is not called by anything; its a special "start here" part of your program.
Goto can be replaced many ways depending on what you want to do. If you goto a label above your current spot, that is a loop. If you go below, it is usually an if statement.
so you get something like
while(1) //start
{
code
}
goto AB1; //ok
AB1: //you went here, ok, this is normal program flow, it does nothing!
the internal labels do not do anything. You need a better example.
now, if they gotos were conditional, a function is good:
if(something)
goto ABC2;
is the same as
if(something)
ABC2(); //function call
or
if(something)
{
code //maybe no need for a function, maybe its just a little code
}
The bottom line is to forget goto when writing c++. Almost everything that can be done in the language is doable another, better way. There used to be 1 thing gotos were used for, and that was error handling of deep nested loops:
I would say that the "goto" statement can be replaced with a while, do/while loop ro a switch/case.
Your second example is a better choice for a program because main calls AB1 which calls AB2 then returns to AB1 which returns to main to end the program. Since this happens calling main on line 38 is not necessary. And likely t lead to an endless loop with no way out.
Technically not need, but good form to put return 0; at the end of main.
You can collapse a lot of this logic and make it elegant and all with some variables and parameters. But I am not able to do that right this second. If you did that you can do all this stuff in one function with a little conditional logic. That isnt important here.
With what you have, you can do this:
int main()
{
start();
}
int start()
{
cout << endl << "Welcome" << endl;
cout << "Type 1 or 2." << endl;
cin >> next;
if (next == '1') AB1();
else if (next == '2') AB2();
}
int AB1()
{
cout << endl << "Welcome to AB1." << endl;
cout << "Type r to return or 2 to enter AB2." << endl;
cin >> next;
if (next == 'r') start();
else if (next == '2') AB2();
}
int AB2()
{
cout << endl << "Welcome to AB2." << endl;
cout << "Type r to return or 1 to enter AB1." << endl;
cin >> next;
if (next == 'r') start();
else if (next == '1') AB1();
}