Thread: String Problem... [sorry this is so lame =(]

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    39

    String Problem... [sorry this is so lame =(]

    actually its my first time with strings and arrays...
    so basically i would want to do something like this...
    P.S (pre-script XD) please do not use advanced commads/functions because i only know up to the array level...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char username[50];
    	char password[50];
    
    	clrscr();
    	printf("\nUsername:\t");
    	gets(username);
    	printf("\nPassword:\t");
    	gets(username);
    
    	if(username == 'ADMIN' && password == 'admin')
    	{
    		clrscr();
    		printf("\nWelcome Administrator!");
    	}
    
    
    	getche();
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    So, what are you trying to do, and what problems are you facing when trying to do it?

    Incidentally, note that gets() leaves you vulnerable to buffer overflow. You can use fgets() instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To compare strings in C, you must use strcmp, which returns 0 if they are equal. Eg:
    strcmp(username, "ADMIN") == 0
    Also note that '' is a single char, and "" is a string.
    Further explanation on gets: http://cpwiki.sourceforge.net/Gets
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    what im doing is to know whether the username and password [as strings] is equal to the word 'ADMIN'... consider it like a login function because the admin account is already predefined...

    How can i compare the content of a string to a WORD?

    another thing, what is the difference between gets() and fgets()?

    thanks anyway sis!
    btw, this is for my project...

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    thanks Elysia... but... how come this doesnt work?

    Code:
    if((strcmp(username, "ADMIN") == 0) && (strcmp(password, "admin") == 0))
    	{
    		clrscr();
    		printf("\nWelcome Administrator!");
    	}
    	else
    		printf("\n\nInvalid username / password.");

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Firstly, it's case sensitive, so ADMIN is not the same as admin.
    Secondly, I figure you didn't strip the newline. If you look at the link for gets, there is an example of how to do that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    In addition, clrscr() is not a standard C function, so your code won't be portable.

    Code:
    printf("\nUsername:\t");
    gets(username);
    printf("\nPassword:\t");
    gets(username);
    That should be password not username.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM