String compare problems

This is a discussion on String compare problems within the C Programming forums, part of the General Programming Boards category; Hey guys I am making a very simple string program to test logical operators and although this compiles its not ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    String compare problems

    Hey guys

    I am making a very simple string program to test logical operators and although this compiles its not giving the correct output.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main ( void ) {
    	char username[ 15 ];
    	char password[ 20 ];
    	int flag = 0;
    
    	puts("Enter your username: ");
    	fgets( username, 15, stdin );
    	puts("Enter your password: ");
    	fgets( password, 20, stdin );
    
    	if (( strcmp( username, "peter") == 0 ) && ( strcmp( password, "willow") == 0)) {
    		puts("\nWelcome to the network!");
    		flag = 10;
    	}
    
    	if (( strcmp( username,  "guest") == 0 ) || ( strcmp( password, "guest")) == 0 ) {
    		puts("\nGuest registration valid for 24hrs");
    		flag = 2;
    	}
    
    	if (!flag ) {
    		puts("\nInvalid input!");
    }
    
    	return 0;
    }
    I keep getting "Invalid input returned when ever i compare the strings, can
    anyone see what I am doing wrong?

    Thank in advance guys.
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    You're not stripping the newlines from fgets() . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Thanks!

    I forgot to add

    Code:
    username[ strlen(username ) -1 ]= 0;
    Cheers Nightowl
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Same thing for password, of course . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    I forgot to add

    Code:
    username[ strlen(username ) -1 ]= 0;
    Cheers Nightowl
    That will fail if the strlen() returns 0 - which would happen if someone enters CTRL-Z or CTRL-D depending on platform.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Use a "chomp" style function:

    Code:
    void chomp (char *line) {
            int len=strlen(line);
            if ((len>0) && (line[len-1]=='\n')) 
    			line[len-1]='\0'; 
    }
    
    chomp(username);
    chomp(password);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21