how main() is call from main()

Jan 3, 2010 at 5:40am
I got confused in a topic that is how main() is called from main()
I know its due to recursion bt how ?

please allustrate ur ansr with an example

Regards
Jan 3, 2010 at 5:42am
You shouldn't call main from anywhere.
Jan 3, 2010 at 6:02am
You shouldn't call main from anywhere.
that is indeed correct

or maybe there is another main() function in your code

post the code you have read to clear this.
Jan 3, 2010 at 6:07am
In C++ it is illegal to call main() explicitly.
Jan 4, 2010 at 9:46am
me too, can someone teach me how to do it,, or just give me a simple example,so that a beginner like me will understand it most..help me experts plss..im new to c++
Jan 4, 2010 at 9:58am
so you mean this code is incorrect?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void userInput(char* checkInput);

int main()
{
	bool correctInput = false;
	char input[80];
	userInput(input);
	correctInput = braceCheck(input);
	if(correctInput == true){
		cout<<"CORRECT!"<<endl;
		main();
	}
	else{
		cout<<"INCORRECT!"<<endl;
		main();
	}

}//end main
void userInput(char* checkInput)
{
	cout<<"Input Anything: ";
	cin>>checkInput;
	if((checkInput[0]=='e')&&(checkInput[1]=='x')&&(checkInput[2]=='i')&&(checkInput[3]=='t')){
		exit (0); //terminates the program
	}//end if
}//end userInput function

//........ 

-----------------------
but what will I do if I want the code to return to userInput() function again?
If I use userInput(input) instead of main() it will only recurse 1 time.
so what is the best solution for this? thx
Jan 4, 2010 at 10:24am
why not just say...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void userInput(char* checkInput);
void runApp();

int main()
{
    runApp();
    return 0;

}//end main
void userInput(char* checkInput)
{
	cout<<"Input Anything: ";
	cin>>checkInput;
	if((checkInput[0]=='e')&&(checkInput[1]=='x')&&(checkInput[2]=='i')&&(checkInput[3]=='t')){
		exit (0); //terminates the program
	}//end if
}//end userInput function

void runApp()
{
        bool correctInput = false;
	char input[80];
	userInput(input);
	correctInput = braceCheck(input);
	if(correctInput == true){
		cout<<"CORRECT!"<<endl;
		runApp();
	}
	else{
		cout<<"INCORRECT!"<<endl;
		runApp();
	}
}
Last edited on Jan 4, 2010 at 10:24am
Jan 4, 2010 at 3:46pm
Both of those code examples are awful

1) Repeatedly calling a function sucks up stack space. Just use a loop.

2) Exiting with exit() is ill advised, IMO. It's best to exit by returning normally from main.

3) Why are newbies so afraid of std::string? char arrays are a lot meaner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
string userInput();

int main()
{
	string input;

	while(true)  // loop indefinitely
	{
		input = userInput();
		if(input == "exit")  // if the user typed "exit"
			break;    // break out of the infinite loop

		// otherwise do the rest:
		if( braceCheck(input) )
			cout << "CORRECT!" << endl;
		else
			cout << "INCORRECT!" << endl;
	}

	return 0;
}//end main

string userInput()
{
	string input;
	cout<<"Input Anything: ";
	cin>>input;

	return input;
}//end userInput function 
Last edited on Jan 4, 2010 at 3:48pm
Jan 4, 2010 at 4:32pm
disch wrote:
Both of those code examples are awful
hahaha lmao

disch wrote:
Why are newbies so afraid of std::string? char arrays are a lot meaner.
yeah i also notice that, maybe because of how most tutorials do.
Jan 4, 2010 at 9:37pm
Explicitly calling main() is always a BAD IDEA.

You just don't know what will happen (since K&R just assumed that no one in their right mind would ever recurse on main). Hence, the C++ standards committee decided to address the issue and expressly forbid it.

Just say "no" to return main();!
Jan 5, 2010 at 12:44am
ok thanks for the idea.
It helps me a lot.
I will now use string instead of char arrays.
Topic archived. No new replies allowed.