Thread: What is wrong with this program??

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    45

    What is wrong with this program??

    hi, im new to programming and was wondering could anyone tell me what is wrong with this program?:

    Code:
    #include <stdio.h>
    
    char set_user, set_pass, user, pass;
    
    main() {
    	printf("Set your Username: ");
    	scanf("%s", &set_user);
    	printf("Set your Password: ");
    	scanf("%s", &set_pass);
    	printf("Username: ");
    	scanf("%s", &user);
    	printf("Password: ");
    	scanf("%s", &pass);
    	if ( set_user == user && set_pass == pass ) {
    		printf("Welcome to your system\n");
    	}
    	else {
    		printf("Username or Password is incorrect\n");
    	}
    }
    Ive tried compiling with: gcc -x c '/root/Program Files/user_and_pass.c'
    and: g++ -x c '/root/Program Files/user_and_pass.c'

    it compiled but when i tried the program
    output:

    Code:
    Set your Username: name
    Set your Password: pass
    Username: name
    Password: pass
    Username or Password is incorrect
    Thanks in advance.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't compare strings with ==. Check out the strcmp() function. Plus, your variable types are incorrect. You need to be using an array of chars instead of a single char (e.g. char set_user[50]).
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    45
    kk, thanks for that

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... a few things....

    You've defined your username, password variables as type char ... this limits you to one character each. You need to set aside memory to hold your strings.

    Code:
    char set_user[32];
    char set_pass[16];
    ...
    If you don't do this you will end up overwriting parts of your program's stack or image which can lead to catastrophic failure... "Seg fault" or "Buffer Overrun"....

    Once you set aside memory properly you will need to change your input statements...
    Code:
    scanf("%s", set_user);
    ... since set_user (etc) are now pointers to arrays of characters.

    Finally C doesn't natively know much about strings. You cannot compare two strings using the == (is equal) operator. C doesn't know how to do that... You will need library functions such as strcmp() and stricmp() for that task.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Maze Program - What am I doing wrong?
    By Gipionocheiyort in forum C++ Programming
    Replies: 20
    Last Post: 08-02-2007, 01:31 PM
  2. Replies: 5
    Last Post: 01-13-2007, 02:14 AM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM