Question about return


1
2
3
4
5
void main(void)
....
....
...
return main();

Can I use return main(); to allow the program to start from the beginning once again? So far when I tried running the program, it allow me to run and then start from the beginning when it reach return main();. However, I had no idea what does return main(); mean. Can anyone explain to me? Thanks.
1) void main() is ileegal in C++ (and in C too). Main should return int:
Standard wrote:
3.6.1.2
An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int


2) You should not call main() in your program:
Standard wrote:
3.6.1.3
The function main shall not be used within a program.
So how do I start from the beginning again? For example, I have a login system such that when the user enter the wrong password 3 time they will have to enter their id again. Below is an example.

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
do
	{
		printf("UserID: ");
		getUserID(userID); // Call function to get the user id
		status = chkUserID(userID); // Call function to check the user id.
		if (status == 0)
		{
			printf("\nIncorrect UserID. Try again.\n");
			continue;
		}
	} while (status == 0);
do
	{
		printf("Password: ");
		getPassword(userPassword);
		try++;
		status = chkPassword(userPassword);
		if (try> 2)
		{
		printf("\n\nYou have used up your 3 password attempts.\n\n");
		return main(); // Start from beginning when try more than 2.
		}
		if (status == 0)
		{
			printf("\nIncorrect Password. Try again.\n");
			continue;
		}
		else
		{
			printf("\n\nLogin Successful.\n");
			printf("Press [Enter] to continue . . .");
			getchar();
		}
	} while (status == 0);

Assuming that the user have typed the correct user id and proceed on with the password, after 3 times of typing the wrong password, it will say they have used up their 3 attempts and then go back to the start where they have to enter their id again. The process then keeps going on and on till they enter the correct password. Is the code above correct? If it's wrong, can you explain to me why and how do I make changes to the code? Thanks. I am only using stdio.h and conio.h in c for this program.
Instead of calling main, wrap your login routine in another loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do {
    do {
        /*Get ID*/
        status = chkUserID(userID);
    } while (status == 0);
    do {
        /*get password*/
        if((status = chkPassword(userPassword)) == 0) {
            printf("\nIncorrect Password. Try again.\n");
            ++attempt; //try is reserved keyword in C++ and you cannot use it
            if (attempt > 2) {
                printf("\n\nYou have used up your 3 password attempts.\n\n");
                break;
            }
        }  
    } while (status == 0);
} while (status == 0);
It really helps to mose some routines to their own function to reduce clutter.
Oh..... Thank you so much for helping me. I didn't think about using two do..while loop in this. Also, I didn't know you can't call main() in the program. Thanks! :)
Last edited on
Topic archived. No new replies allowed.