main(); called from another function fails

Totally lost, i get a "Compiler Error C3861" after trying to restart the app if the end user doesn't want to end the app. Ending seems ok, but the continuing seems to cause this error when i add the code 'main();' - I have also noticed that adding the #includes in the wrong order can be a problem, and i wonder if i have gone wrong here too?

// Please Note: The following headers do have a purpose: - as for doubling up on one with equally good enough purpose, i am un-sure..

#include "windows.h"
#include <filesystem>
#include "stdio.h"
#include "iostream"
#include "conio.h" // Headers don't work unless they are placed after the.. above ones
#include "tchar.h"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 int exit_app()
{
	int ch;

	_cputs("\n\n'Q' to Exit or 'C' to continue: ");
	do
	{
		ch = _getch();
		ch = toupper(ch);
	} while ((ch != 'Q') && (ch != 'C'));

	if (ch == 'C')
	 main(); // POINT OF FAIL C3861 **

	// _putch is the command that makes the activations of a command appear seamless
	_putch(ch);
	_putch('\r');    /* Carriage return : exacts the 'Q' quit command by automatically entering my 'Q'
					 allowing the function to end with return 0; */
	_putch('\n');    // again new line instigated by _putch - i love it :-D
	

	return 0;
}
Last edited on
main is the entry point of a program. It shall not be called by any part of the program itself. Doing so is illegal and produces undefined behavior.

Don't manually call main. Best practice would be to avoid recursion by using loops. If this is infeasible, then make main call "fake_main" and recursively call fake_main instead.

1
2
3
4
int main()
{
    fake_main();
}


But really, restructure your program to properly use loops. You should never have to call main or a "pseudo"-main recursively.
Last edited on
Thank you :-)
Topic archived. No new replies allowed.