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;
        password[9] =basketball;
        
        printf("Enter your password:");
        scanf("%s", &pass[9]);
        
        if (pass[9] == password[9])
        {
                 printf("ACCESS GRANTED\n");     
        }
        
        else
        {
             printf("ACCESS DENIED\n");
        }
        
        
        
        scanf("%d", keepalive); // keeps the program running
    }
    is there places is wrong?
    the statement look correct to me.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    In short,, it's all wrong.

    1. This is C code, not C++.
    2. You do not compare character arrays with ==, you do so with strcmp. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    3. You don't seem to understand character arrays at all. You assign the value stored in the variable basketball (which contains a single character, but you don't know what) to the password array element one after the end of the array (arrays are indexed starting at 0, so you can only safely access elements 0-8). This is writing into memory you don't own, which is undefined behavior.

    Read this: http://www.cprogramming.com/tutorial/lesson9.html

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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 showing SIG fault when running with gdb
    By SasDutta in forum C Programming
    Replies: 3
    Last Post: 04-13-2011, 09:15 AM
  2. Program not showing up
    By bijan311 in forum Windows Programming
    Replies: 4
    Last Post: 10-08-2010, 06:53 AM
  3. program that prints two tables showing...
    By Cyberman86 in forum C Programming
    Replies: 8
    Last Post: 04-08-2009, 01:55 PM
  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