Jun 20, 2015 at 8:09pm Jun 20, 2015 at 8:09pm UTC
No, it is never okay to call main(). main is a special function that is only the entry point to the program. Use a while loop inside main instead.
rough example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
int main()
{
Listthenodes Justin;
int Bo;
while (true ) {
int x;
cout << "1 . Add to node" << endl;
cout << "2 . Delete a node" << endl;
cout << "3 . Display nodes" << endl;
cout << "4 . Exit program" << endl;
cin >> x;
switch (x)
{
case 1:
cout << "Add an node" << endl;
cin >> Bo;
Justin.Addnode(Bo);
continue ; // goes to top of while loop again
}
}
}
Last edited on Jun 20, 2015 at 8:17pm Jun 20, 2015 at 8:17pm UTC
Jun 20, 2015 at 8:14pm Jun 20, 2015 at 8:14pm UTC
Is return Main() a good idea?
No - it's a bad idea.
The C++ Standard actually says that you may not call main() from your own code. But as C allows it is seems that compilers let you get away with it.
Is there another way to do this?
Yes.
Here you should use a while loop.
(But if you do come across an actual need for a recursive main, move the contents out of main into your own function and then call it recursively.)
Andy
Last edited on Jun 20, 2015 at 8:25pm Jun 20, 2015 at 8:25pm UTC
Jun 20, 2015 at 8:18pm Jun 20, 2015 at 8:18pm UTC
@andywestken @Ganado
Thanks.