Thread: Password check

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134

    Password check

    Hey guyz, I've been doing my school project and I cant seem to get the login function to work properly but it just keeps telling that the password is wrong. I wrote several other programs to check is the program works correctly and it work in all the other but not in my project.

    Code:
    if(eNum <= cAccNum + 1)
    		{
    			fseek(fPtr, (eNum - 1) * sizeof(struct acc), SEEK_SET);
    			fread(&lAcc, sizeof(struct acc), 1, fPtr);
    			printf("Enter your password %s: ", lAcc.name);
    			scanf("%s", pass);
    			if(strcmp(pass, lAcc.password) != 1)
    			{
    				log = 1;
    				printf("%d\n", strcmp(pass, lAcc.password) != 0);
    				printf("1\n");
    			}
    			else
    			{
    				for(x = 0; x < strlen(pass); x++)
    				{
    					if(pass[x] != lAcc.password[x])
    					{
    						log = 1;
    						printf("2");
    						break;
    					}
    				}
    			}
    			if(log == 0)
    			{
    				printf("\n\nSuccessful login...\n");
    			}
    			else
    			{
    				printf("\n\nInvalid password!\n");
    			}
    		}
    		else
    		{
    			log = 1;
    			printf("\nAccount was not found.\n");
    		}
    Can some1 else me please!! I nothing some bugs in it but the ones that I found aren't causing the problem. and I notice the strcmp only return 1 and nothing else.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you sure that lAcc.password is what you think it is? Why not, for debugging purposes, print things out (like the value of pass, and lAcc.password, and log, etc) or watch them in a debugger?

  3. #3
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    Password check-password-jpg

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    if(something) {
        log = 1;
    else 
       log = 1;
    
    if(log==0)
        login successful
    
    ??????
    
    
                if(strcmp(pass, lAcc.password) != 1)
                {
                    log = 1;
                    printf("%d\n", strcmp(pass, lAcc.password) != 0);
                    printf("1\n");
                }
                else
                {
                    for(x = 0; x < strlen(pass); x++)
                    {
                        if(pass[x] != lAcc.password[x])
                        {
                            log = 1;
                            printf("2");
                            break;
                        }
                    }
                }
                if(log == 0)
                {
                    printf("\n\nSuccessful login...\n");
                }
                else
                {
                    printf("\n\nInvalid password!\n");
                }
            }

  5. #5
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    huh?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BIGDENIRO View Post
    huh?
    First, I can't read your pic's. Way too small.

    The way you handle log, appears to ensure you can't have a valid login.

    The if and the else part should not assign the same log value.

    strcmp() should return 0,-1 (or a negative number), or 1 (or a positive number), depending on the relative value of the left most string:

    int n = strcmp(left string, right string);

  7. #7
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    Both statement set log to 1 if the password is wrong in both cases and the strcmp is where my problem lies it only return -1

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, fun fact: You don't print any spaces between the password (on line 1560) and the prompt (on line 1561). Yet in the screenshot there are six spaces in between them. I therefore conclude that the password you have stored ends in six spaces.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BIGDENIRO View Post
    Both statement set log to 1 if the password is wrong in both cases and the strcmp is where my problem lies it only return -1

    That means string1 is less than string2. Perhaps you have an extra newline char at the end of the string? Or maybe one of your strings is lacking the end of string marker char?

    password\n0 instead of
    password0

    Where the 0 represents the end of string marker.

    Sometimes these newlines and end of string chars, since they can't be seen easily, produce these kind of weird errors in our programs.

  10. #10
    Registered User
    Join Date
    Sep 2013
    Location
    Jamaica
    Posts
    134
    ty for da tip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Password check
    By jgkamin in forum C Programming
    Replies: 16
    Last Post: 02-19-2013, 07:55 PM
  2. Password check with 3 attempts
    By samwillc in forum C++ Programming
    Replies: 6
    Last Post: 06-09-2012, 03:54 AM
  3. How to check somebody's password?
    By marinus in forum Linux Programming
    Replies: 8
    Last Post: 09-10-2005, 10:17 AM
  4. Simple login/password check
    By ceevee in forum C++ Programming
    Replies: 26
    Last Post: 01-17-2005, 12:05 AM
  5. Check if input password is correct
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 09:28 AM