#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0

    Problem With NestedIf


    Hi guys, I'm new here and I thought of seeking some help regarding my program.

    I made this program where it asks for a username and password for access. The username works well, but whenever I enter the password I programmed, it always says "Access Granted! Welcome." Is there anyone who can help me? Here's my code and Thanks in advance!

    Code:
    #include<stdio.h>
    #include<conio.h> 
    #include<stdlib.h> 
    #include<string.h> 
    main() 
    {     
       char a[20],c[20];     
       clrscr();     
      printf("Enter your username: ");     
      gets(a);     
      printf("\n Enter your password: ");     
      gets(c);    
      if(strcmp(c, "apple"))    
     {         
        printf("\n Access Granted! Welcome %s!",a);  
     }     
        else if (strcmp(c, "mango"))     
    {         
        printf("\n Access Granted! Welcome %s!",a);     
    }     
        else if (strcmp(c, "banana"))     
    {         
        printf("\n Access Granted! Welcome %s!",a);     
    }     
        else     
    {         
       printf("\n Access Denied!");    
    }     
      getch();     
    return 0; 
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0
    I checked before post...It's Working For the Given Values...But It does n't Show the Output Like "Access Denied!" when Entering the Wrong Password
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    1. Stop using gets - read this

    > if(strcmp(c, "apple"))
    2. Did you read the manual page for strcmp() before trying to use it?

    Did you see the bit which says that the return result is ZERO when the strings MATCH?

    Try say
    if(strcmp(c, "apple") == 0 )

    Comments on this post

    • Invalid Loop! agrees
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0
    After Making Changes To My Code, I Found Only "Access Denied!" Message..

    Code:
    #include<stdio.h> 
    #include<conio.h> 
    #include<stdlib.h> 
    #include<string.h> 
    main() 
    {     
       char a[20],c[20];     
       clrscr();    
       printf("Enter your username: ");    
      *fgets(a,sizeof a,stdin);     
       printf("\nEnter your password: ");    
      *fgets(c,sizeof c,stdin);     
       if(strcmp(c, "apple") ==0)     
    {        
        printf("\nAccess Granted! Welcome %s!",a);   
    }    
       else if (strcmp(c, "mango") ==0)    
    {         
        printf("\nAccess Granted! Welcome %s!",a);     
    }     
        else if (strcmp(c, "banana") ==0)     
    {         
         printf("\nAccess Granted! Welcome %s!",a);     
    }     
         else    
    {         
        printf("\nAccess Denied!");     
    }    
         getch();     
         return 0; }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0
    1)Yes!! That Is the Better way to Go Alternative For gets()
    with fgets()
    2)After Changes also My Code does n't work Proper :confused
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Read the FAQ again, and scroll down to this...
    The only caveat with fgets() is that it does not remove the (possible) newline from the end of the buffer like gets() does. This is easily remedied, however, in several different ways. As an example:
    ... and read on.

    Also, please stop typing English where you capitalise every single word.
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    On Earth!!
    Posts
    9
    Rep Power
    0
    Originally Posted by salem
    Read the FAQ again, and scroll down to this...

    ... and read on.

    Also, please stop typing English where you capitalise every single word[/URL].
    i did it..Thank You Salem Sir :cadrunk:

    Comments on this post

    • salem agrees : Good job, well done

IMN logo majestic logo threadwatch logo seochat tools logo