Thread: How do i create this code into a function?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    45

    How do i create this code into a function?

    Hi Guys,

    so sorry to create a new thread.

    But just wondering...

    how do i create a function of checkpw() with this bunch of codes...

    Code:
       while( fgets(line_buffer,sizeof(line_buffer),fpassword) != NULL )  /* read from file while still data available */
         {
    
              char ftemp[20], fpass[20], fuser[20];
    
              sscanf(line_buffer,"%s %s %s",&ftemp,&fpass,&fuser);
    
    
              if (strcmp(user, fuser) == 0) /* OK, found the username the user inputted */
              {
                 printf("\n\nUsername Correct\n");
    
                 if (strcmp(pass, fpass) == 0)
                 {
    		         printf("User authorized");
                     getchar();
                     exit(1);
                 }
              }
         }
        printf("\n\nInvalid Username or Password");
        fclose(fpassword);
        getchar();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well first you decide what would be the useful parameters of the function (say the password filename), and what can be just local variables (say ftemp).

    Then you decide that exit(1) is a bad thing to do, and invent some status returns for the function, say
    - pass
    - fail
    - file not found

    > sscanf(line_buffer,"%s %s %s",&ftemp,&fpass,&fuser);
    There's no need (it's also wrong) to use & when the target is a char array, and the format is %s. It's one of the few exceptions.
    Also checking the return result is a good idea as well.
    Using a field width in the conversion makes it even better.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Salem View Post
    Well first you decide what would be the useful parameters of the function (say the password filename), and what can be just local variables (say ftemp).

    Then you decide that exit(1) is a bad thing to do, and invent some status returns for the function, say
    - pass
    - fail
    - file not found

    > sscanf(line_buffer,"%s %s %s",&ftemp,&fpass,&fuser);
    There's no need (it's also wrong) to use & when the target is a char array, and the format is %s. It's one of the few exceptions.
    Also checking the return result is a good idea as well.
    Using a field width in the conversion makes it even better.
    i did this and doesn't work why....

    Code:
    ......
    
    checkpw();
    
    ......
    
    char checkpw(char pass, char fpass)
    {
          if(strcmp(pass, fpass) == 0)
          {
    		         printf("User authorized");
                     getchar();
                     exit(1);
          }
          printf("\n\nInvalid Username or Password");
          getchar();
          exit(1);
    }
    how come doesn't work.....

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look at the declaration of say strcmp() and how strings are passed from one function to another. Then compare with the declaration of your function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    also it might be safer putting either the function ahead of your main or at least putting a function prototype up there, but i guess that depends on your compiler

  6. #6
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    depending on the intended length of passwords and the purpose you might also consider converting the parameter passing to pointer passing, since your not actually operating on either value
    Code:
    char *pass;
    char *fpass;
    
    ...
    
    checkpw(pass, fpass);
    
    ...
    char checkpw(char *pass, char *fpass)
    {
    ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM