Thread: program keep showing denied

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    program keep showing denied

    Code:
    #include <stdio.h>
    
    
    int main(){
        char pass[9], password[9],basketball;
        int keepalive=0;
        int i;
        password[9]=basketball;
        
        printf("Enter your password:");
        scanf("%s", &pass[9]);
        
        for(i=0; i<9; i++)
        {
          if (pass[i]==password[i])  
           {             
              printf("ACCESS GRANTED\n");
           }
              
          else
          {
                 printf("ACCESS DENIED”\n");       
          }      
              
              scanf("%d", keepalive); // keeps the program running
              
        }
    }


    it shows denied no matter what i typed

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    First, get and use a compiler with a good range of diagnostics.

    $ gcc -Wall bar.c
    bar.c: In function ‘main’:
    bar.c:25: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    bar.c:28: warning: control reaches end of non-void function
    bar.c:8: warning: ‘basketball’ is used uninitialized in this function


    For example, declaring a variable with a name doesn't make it equal that name.
    So you need something like
    char password[ ] = "basketball";

    You should also note that this contains more than 9 characters.

    Further, you should have something like
    scanf("%s", guess);

    Where guess is a char array of sufficient size.
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by crimisonfray View Post
    Code:
    #include <stdio.h>
    
    
    int main(){
        char pass[9], password[9],basketball;
        int keepalive=0;
        int i;
        password[9]=basketball;
        
        printf("Enter your password:");
        scanf("%s", &pass[9]);
        
        for(i=0; i<9; i++)
        {
          if (pass[i]==password[i])  
           {             
              printf("ACCESS GRANTED\n");
           }
              
          else
          {
                 printf("ACCESS DENIED”\n");       
          }      
              
              scanf("%d", keepalive); // keeps the program running
              
        }
    }


    it shows denied no matter what i typed
    In addition to Salem's comments, you should note that line 15 is going to fail no matter what you do, since C cannot compare strings across the == operator. You will need strcmp() for that.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > you should note that line 15 is going to fail no matter what you do, since C cannot compare strings across the == operator.
    They're comparing chars, not strings.
    So == is appropriate.

    However, using strcmp() would simplify the code no end.

    Especially since the current compare logic is to print "access granted" just by guessing the first char.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program keep showing denied
    By crimisonfray in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2011, 05:50 AM
  2. Program showing SIG fault when running with gdb
    By SasDutta in forum C Programming
    Replies: 3
    Last Post: 04-13-2011, 09:15 AM
  3. Program not showing up
    By bijan311 in forum Windows Programming
    Replies: 4
    Last Post: 10-08-2010, 06:53 AM
  4. Program showing splitter
    By Zurswe in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2009, 09:33 AM
  5. A C++ program examples showing Polymorphism, please help.
    By MarkSquall in forum C++ Programming
    Replies: 19
    Last Post: 06-06-2008, 04:41 AM