Thread: add the log in function in main

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    48

    add the log in function in main

    Code:
    login()
    {
    	char username[15];
    	char password[12];
    
    
    
    
    	printf("Enter your username:\n");
    	scanf("%s", &username);
    
    
    	printf("Enter your password:\n");
    	scanf("%s", &password);
    
    
    	if (strcmp(username, "chaitu") == 0) {
    		if (strcmp(password, "123") == 0) {
    
    
    			printf("\nWelcome.Login Success!");
    
    
    		}
    		else {
    			printf("\nwrong password");
    		}
    	}
    	else {
    		printf("\nUser doesn't exist");
    		system("exit");
    		
    	}
    
    
    
    
    
    
    
    
    
    
    
    
    }
    void main()
    {
    	login();
    	int option;
    	
    
    
    	do {
    		printf("\nStaff MENU\n\n");
    		printf("\t1 -> Display Staff Details\n");
    		printf("\t2 -> Add Staff\n");
    		printf("\t3 -> Modify Staff\n");
    		printf("\t4 -> Delete Staff\n");
    		printf("\t5 ->Search Staff\n");
    		printf("\t 6-> Exit\n");
    		printf(" > ");
    		rewind(stdin);
    		scanf("%d", &option);
    
    
    		switch (option) {
    		case 1: displayStaff(); break;
    		case 2: addStaff(); break;
    		case 3: modifyStaff(); break;
    		case 4: deleteStaff(); break;
    		case 5: searchStaff();
    		case 6: break;
    		default:
    			printf("Invalid choice....\n\n");
    		}
    	} while (option != 6);
    	system("pause");
    }
    the problem is even though user enter the wrong password or username, it will still display the menu..

  2. #2
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    and make the password display as ***

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Code:
    char username[15];
    char password[12];
    
    // ...
    
    scanf("%s", &username);
    
    // ...
    
    scanf("%s", &password);
    You don't need the '&' here. The name of the array by itself acts as a pointer to its first element.
    Compile with full warnings to find problems like this.

    Other notes:


  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by kkkcj View Post
    and make the password display as ***
    You can't using standard C, but there are ways.
    There's an entry in the FAQ that deals with this.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int login()
    {
        char username[15];
        char password[12];
     
        printf("Enter your username:\n");
        //scanf("%s", &username);
        scanf("%s", username);
     
     
        printf("Enter your password:\n");
      //  scanf("%s", &password);
        scanf("%s", password);
     
     
        if (strcmp(username, "chaitu") == 0) {
            if (strcmp(password, "123") == 0) {
     
     
                printf("\nWelcome.Login Success\nd!");
                return 0;
     
     
            }
            else {
                printf("\nwrong password\n");
                return -23;
            }
        }
        else {
            printf("\nUser doesn't exist\n");
            //system("exit");
            return -44; 
             
        }
    }
    
    void menu (void)
    {
        
        int option = 0;
        
        // do while loops always run at lest once before exiting
        // you do not have to change your code this is just
        // a representation of how it could look
        
        while (option != 6 );
        {
             printf("here\n");
             
            printf("\nStaff MENU\n\n");
            printf("\t1 -> Display Staff Details\n");
            printf("\t2 -> Add Staff\n");
            printf("\t3 -> Modify Staff\n");
            printf("\t4 -> Delete Staff\n");
            printf("\t5 ->Search Staff\n");
            printf("\t 6-> Exit\n");
            printf(" > ");
            //rewind(stdin);
             printf("here\n");
            scanf("%d", &option);
            
            // commented out due to 
            // lack of functions in switch
     
     /*
            switch (option) {
            case 1: displayStaff(); break;
            case 2: addStaff(); break;
            case 3: modifyStaff(); break;
            case 4: deleteStaff(); break;
            case 5: searchStaff();
            case 6: break;
            default:
                printf("Invalid choice....\n\n");
            }
            */
             printf("here\n");
        } 
       // system("pause");
        
        
        
    }
    //void main() this is so not I changed it. 
    int main(void)
    {
          
       
        int flag = login();
        
        if ( flag != -44 && flag != -23)
            menu();
        else
            printf("flag %d\n\n", flag);
        
       
        return 0;
    }
    as far as making the password not show up in Linux it got a be Turing off the terminal something something as I just briefly looked at su source code for their function called getpass within it then look at that functions source code and seen what they did, which was turn off the terminal . but it looks like this is really old code or you have not been properly informed about the wrongful usage of declaring main.

    The GNU C Library: getpass

    Code:
    //wrong
    void main()
    //right
    int main(void)
    {
    
    return 0;
    }
    with the use of your getch and not getchar I'd say that is old windows or just windows stuff. so getpass turns off the terminal might not work for you, DOS box, or what is Windows calling it now days? you might look into how to manipulate that to not show the password. But windows is not open source so .. searching searching searching maybe be fun for you.

    if you where doing GUI their is easier ways to hide password.
    Last edited by userxbw; 12-12-2017 at 11:28 AM.

  6. #6
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    i try..but if user name and password both correct it only show the Welcome.Login Success and won't show the menu

  7. #7
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int login()
    {
        char username[15];
        char password[12];
     
        printf("Enter your username:\n");
        scanf("%s", username);
     
     
        printf("Enter your password:\n");
        scanf("%s", password);
     
     
        if (strcmp(username, "chaitu") == 0) {
            if (strcmp(password, "123") == 0) {
     
     
                printf("\nWelcome.Login Success\nd!");
                return 0;
     
     
            }
            else {
                printf("\nwrong password\n");
                return -23;
            }
        }
        else {
            printf("\nUser doesn't exist\n");
            return -44; 
             
        }
    }
    
    void menu (void)
    {
        
        int option = 0;
        
        while (option != 6 );
        {
             printf("here\n");
             
            printf("\nStaff MENU\n\n");
            printf("\t1 -> Display Staff Details\n");
            printf("\t2 -> Add Staff\n");
            printf("\t3 -> Modify Staff\n");
            printf("\t4 -> Delete Staff\n");
            printf("\t5 ->Search Staff\n");
            printf("\t 6-> Exit\n");
            printf(" > ");
            //rewind(stdin);
             printf("here\n");
            scanf("%d", &option);
            
            // commented out due to 
            // lack of functions in switch
     
    
             printf("here\n");
        } 
         
        
        
    }
     
    int main(void)
    {
          
       
        int flag = login();
        
        if ( flag != -44 && flag != -23)
            menu();
        else
            printf("flag %d\n\n", flag);
        
       
        return 0;
    }
    like this right?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    -23 and -44 are magic numbers: a reader seeing them in the code likely has no idea what they mean without examining further code carefully, and even then it is possible to misconstrue their meaning. As such, you should use named constants instead, e.g.,
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    enum
    {
        LOGIN_SUCCESSFUL = 0,
        LOGIN_FAILURE_WRONG_PASSWORD = -23,
        LOGIN_FAILURE_NO_SUCH_USER = -44
    };
    
    int login()
    {
        char username[15];
        char password[12];
    
        printf("Enter your username:\n");
        scanf("%s", username);
    
        printf("Enter your password:\n");
        scanf("%s", password);
    
        if (strcmp(username, "chaitu") == 0) {
            if (strcmp(password, "123") == 0) {
                printf("\nWelcome.Login Success\nd!");
                return LOGIN_SUCCESSFUL;
            }
            else {
                printf("\nwrong password\n");
                return LOGIN_FAILURE_WRONG_PASSWORD;
            }
        }
        else {
            printf("\nUser doesn't exist\n");
            return LOGIN_FAILURE_NO_SUCH_USER;
        }
    }
    
    void menu(void)
    {
        int option = 0;
    
        while (option != 6);
        {
            printf("here\n");
    
            printf("\nStaff MENU\n\n");
            printf("\t1 -> Display Staff Details\n");
            printf("\t2 -> Add Staff\n");
            printf("\t3 -> Modify Staff\n");
            printf("\t4 -> Delete Staff\n");
            printf("\t5 ->Search Staff\n");
            printf("\t 6-> Exit\n");
            printf(" > ");
            //rewind(stdin);
             printf("here\n");
            scanf("%d", &option);
    
            // commented out due to
            // lack of functions in switch
    
             printf("here\n");
        }
    }
    
    int main(void)
    {
        int flag = login();
    
        if (flag == LOGIN_SUCCESSFUL)
            menu();
        else
            printf("flag %d\n\n", flag);
    
        return 0;
    }
    I took the liberty of changing the flag comparison because if you only want to print the menu on successful login, then that's what you should check for, not check that it is not some list of possible failure conditions, otherwise should another failure condition be added, the menu will be printed should that failure condition occur, if you forget to update the code in main to account for it.

    Other things to think about:
    • Your use of scanf in login is vulnerable to buffer overflow because you read using %s without a field width, so the user could enter more characters than can be stored in the respective arrays. You could use a field width, e.g., %14s for username, or perhaps use fgets (but then keep in mind the newline character from entering the input may need to be removed).
    • You should check the return value of scanf.
    • It looks like in menu, you have a typo error near the start of the while loop concerning an extra semi-colon.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    enum
    {
    	LOGIN_SUCCESSFUL = 0,
    	LOGIN_FAILURE_WRONG_PASSWORD = -23,
    	LOGIN_FAILURE_NO_SUCH_USER = -44
    };
    
    
    int login()
    {
    	char username[15];
    	char password[12];
    
    
    	printf("Enter your username:\n");
    	scanf("%14s", username);
    
    
    	printf("Enter your password:\n");
    	scanf("%11s", password);
    
    
    	if (strcmp(username, "chaitu") == 0) {
    		if (strcmp(password, "123") == 0) {
    			printf("\nWelcome.Login Success\nd!");
    			return LOGIN_SUCCESSFUL;
    		}
    		else {
    			printf("\nwrong password\n");
    			return LOGIN_FAILURE_WRONG_PASSWORD;
    		}
    	}
    	else {
    		printf("\nUser doesn't exist\n");
    		return LOGIN_FAILURE_NO_SUCH_USER;
    	}
    }
    
    
    void menu(void)
    {
    	int option = 0;
    
    
    	while(option != 6);
    	{
    		printf("here\n");
    
    
    		printf("\nStaff MENU\n\n");
    		printf("\t1 -> Display Staff Details\n");
    		printf("\t2 -> Add Staff\n");
    		printf("\t3 -> Modify Staff\n");
    		printf("\t4 -> Delete Staff\n");
    		printf("\t5 ->Search Staff\n");
    		printf("\t 6-> Exit\n");
    		printf(" > ");
    		//rewind(stdin);
    		printf("here\n");
    		scanf("%d", &option);
    		return 0;
    
    
    		// commented out due to
    		// lack of functions in switch
    
    
    		printf("here\n");
    	}
    }
    
    i already change the code but it still won't display the menu after user enter the right username and password..it only display the welcome message.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You didn't post the main function. Be sure to post the entire code.

  11. #11
    Registered User
    Join Date
    Jul 2017
    Posts
    48
    Code:
    int main(void)
    {
    	int flag = login();
    
    
    	if (flag == LOGIN_SUCCESSFUL)
    		menu();
    	else
    		printf("flag %d\n\n", flag);
    
    
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Did you miss this from post #8?

    Quote Originally Posted by laserlight View Post
    It looks like in menu, you have a typo error near the start of the while loop concerning an extra semi-colon.
    Note also that, in "menu()", you're returning zero even though the function's return type is void.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by kkkcj View Post
    like this right?
    never mind,
    Last edited by userxbw; 12-13-2017 at 09:32 AM.

  14. #14
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by laserlight View Post
    -23 and -44 are magic numbers: a reader seeing them in the code likely has no idea what they mean without examining further code carefully, and even then it is possible to misconstrue their meaning. As such, you should use named constants instead, e.g.,
    I took the liberty of changing the flag comparison because if you only want to print the menu on successful login, then that's what you should check for, not check that it is not some list of possible failure conditions, otherwise should another failure condition be added, the menu will be printed should that failure condition occur, if you forget to update the code in main to account for it.

    Other things to think about:
    • Your use of scanf in login is vulnerable to buffer overflow because you read using %s without a field width, so the user could enter more characters than can be stored in the respective arrays. You could use a field width, e.g., %14s for username, or perhaps use fgets (but then keep in mind the newline character from entering the input may need to be removed).
    • You should check the return value of scanf.
    • It looks like in menu, you have a typo error near the start of the while loop concerning an extra semi-colon.
    code from post #5
    I used the given ability to use whatever number one wants to return as a value to use because there is a reason that it is allowed. It gives the him something more to think about. that is one reason I did that too, to try and promote thought that leads to questions that hopefully leads to greater learning and understanding.

    one reason It is allowed so one can write their own error return values in more complex code. something to look forward to. Nothing really wrong with that, because he or she would be doing as you said, "examining further code carefully," instead of just copy paste, turn in, don't have any idea how that code works. scenario, and even if it is not home work that information too is important, to know that one can return anything and put it to their use within their program. It gave him and others a showing of that in working order, that too is why I coded it like that. you just took it to another level.

    said to give understanding, anyways, I just wrote it to do what he could not, stop the menu from showing up whenever. Your improvements are greatly appreciated I am sure by the one that is doing this.

    comparison logic now shows both ways, something more learned by example?
    Last edited by userxbw; 12-13-2017 at 09:37 AM.

  15. #15
    Registered User
    Join Date
    Dec 2017
    Posts
    1,191
    userxbw is simply unable to learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  2. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  3. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  4. Replies: 35
    Last Post: 12-01-2011, 08:31 PM
  5. Replies: 3
    Last Post: 06-01-2011, 03:08 AM

Tags for this Thread