Thread: Login Function

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    10

    Login Function

    ok i've made a login function which checks the username and password before allowing user to enter the main menu.. i was able to make it check the password.. but not the username... error given was.. "Cannot convert "int" to "char *" in function main().. could anyone help ?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    could you post some code? it sounds like your using a int for the username varible and not a char
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    i'm using string ... heres the code

    Code:
    void main(void)
    {
    
    char user[8];
    int password,valid,Try=1;
    
    do{
    	clrscr();
    	printf("Control Panel");
                    printf("Username : ");
    	fflush(stdin);
    	gets(user);
    	gotoxy(25,8);
    	printf("Password : ");
    	scanf("%i",&password);
    	if(password != 888 && user != 'geekz' )
    	{
    		gotoxy(25,10);
    		printf("Invalid Username/Password Entered.");
    		valid = 0;
    		Try += 1;
    	}
    	else
    		valid=1;
    	}while(!valid && Try <=3 );
    	if(Try>3)
    		printf("\n\t\t\tMaximum 3 try only!");
    	else
    		printf("\n\n\t\t\tAccess Approved.");
    		menu();
    }

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Code:
    void main(void)//bad don't void main
    {
    
    char user[8];
    int password,valid,Try=1;
    
    do{
    	clrscr();//im guessing your using conio.h 
    	printf("Control Panel");
                    printf("Username : ");
    	fflush(stdin);//don't do this bad idea
    	gets(user);//try scanf instead
    	gotoxy(25,8);
    	printf("Password : ");
    	scanf("%i",&password);
    	if(password != 888 && user != 'geekz' )
    	{
    		gotoxy(25,10);
    		printf("Invalid Username/Password Entered.");
    		valid = 0;
    		Try += 1;
    	}
    	else
    		valid=1;
    	}while(!valid && Try <=3 );
    	if(Try>3)
    		printf("\n\t\t\tMaximum 3 try only!");
    	else
    		printf("\n\n\t\t\tAccess Approved.");
    		menu();
    }
    check this on why fflush(stdin) is bad
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Woop?

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    oh and this
    Code:
    if(password != 888 && user != 'geekz' )
    try this
    Code:
    if(password != 888 && strcmp(user,"geekz")==0)
    Woop?

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    yup i'm using conio.h.. one problem.. it still only checks the password but not the username...

    nvm i fixed it changed the == to != and it works... thanks a million
    Last edited by magicz69; 07-17-2004 at 06:38 AM.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    10
    i haf another question.. issit possible to compare(strcmp() a string and a int ? as i am trying to make a search as to search by ID(int)...heres the code

    Code:
    void search(void)
    {
    	if((cusfile = fopen("a:\\customer.dat","r"))== NULL)
    			printf("There Are Currently No Customer In The Database");
    	else
    	{
    		clrscr();
    		printf("Enter ID/Name : ");
    		fflush(stdin);
    		gets(Target);
    		while(!feof(cusfile) && Found == 0 )
    		{
    			fscanf(cusfile, "%s , customer.name);
    				if(strcmp(Target,cus.cus_id) == 0 || strcmp(Target,cus.cus_name) == 0 )
    			Found = 1;
    		}
    		if (Found)
    		{
    		printf("ID : %04i",customer.id);
    		printf("Name : %s",customer.name);

  8. #8
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Wow, that code uses just about every evil construct that we violently attack on a daily basis.

    >issit possible to compare(strcmp()) a string and a int ?
    Not with strcmp directly. You could convert the int to a string or the string to an int and then do the comparison. But it looks like you want the user to be able to enter either an ID or a name. In that case it might be easier to try to convert and use that as your validation:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define CORRECT_ID   12345
    #define CORRECT_NAME "Robin Cole"
    
    int main(void)
    {
      char buffer[BUFSIZ];
    
      printf("Enter your User ID or User Name: ");
      fflush(stdout);
      if (fgets(buffer, sizeof buffer, stdin)) {
        char          *endp;
        char          *newline;
        unsigned long  id;
        newline = strchr(buffer, '\n');
        if (newline) {
          *newline = '\0';
        }
        id = strtoul(buffer, &endp, 0);
        if (endp == buffer) { /* User Name was entered */
          if (strcmp(buffer, CORRECT_NAME) == 0) {
            printf("Logging in...\n");
          }
          else {
            printf("Sorry, incorrect User Name\n");
            return EXIT_FAILURE;
          }
        }
        else {                /* User ID was entered */
          if (id == CORRECT_ID) {
            printf("Logging in...\n");
          }
          else {
            printf("Sorry, incorrect User ID\n");
            return EXIT_FAILURE;
          }
        }
      }
    
      return EXIT_SUCCESS;
    }

  9. #9
    Quote Originally Posted by magicz69
    i'm using string ... heres the code
    If you insist in writing non portable Borland C code, try to do it the right way (don't mix direct I/O accesses and streamed I/O accesses)
    Read more about Borland's cgets() for strange details of this code.

    Application security issue: I have changed the strategy of getting the password. It's safer to deal with a raw string than with some numerical value.
    Code:
    #include <conio.h>
    #include <string.h>
    
    #define EOL "\n\r"
    
    int main (void)
    {
       int valid;
       int Try = 1;
       do
       {
          char user[2 + 8];
          char password[2 + 8];
    
          clrscr ();
          cprintf ("Control Panel" EOL);
          cprintf ("Username : ");
    
          user[0] = sizeof user;
          cgets (user);
    
          gotoxy (25, 8);
          cprintf ("Password : ");
    
          password[0] = sizeof password;
          cgets (password);
    
          if (!(strcmp (user+2, "geekz") == 0 && strcmp (password+2, "888") == 0))
          {
             gotoxy (25, 10);
             cprintf ("Invalid Username/Password Entered.");
             valid = 0;
             Try += 1;
          }
          else
             valid = 1;
       }
       while (!valid && Try <= 3);
    
       gotoxy (25, 10);
       clreol();
       if (Try > 3)
          cprintf ("Maximum 3 try only!");
       else
          cprintf ("Access granted.");
    
       return 0;
    }
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM