Thread: Whats wrong?

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99

    Whats wrong?

    Can anyone help me find out whats wrong with this? It builds successfully but when you try to use it does something unusual.




    Write a program that uses function strcmp to compare two strings input by the user. The program should state whether the first string is less than, equal to or greater than the second string.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    int main() {
    
    	char s1[100];
    	char s2[100];
    	
    	printf("Input first string:");
    	gets(s1);
    
    	printf("Input second string:");
    	gets(s2);
    
    	while(s1 != NULL){
    		 strcmp(s1,s2);
    	if(strcmp == 0)
    		printf("These strings are equal");
    	if(strcmp > 0)
    		printf("The first string is larger");
    	if(strcmp < 0)
    		printf("The second string is larger");
    }
    	s1 == "\0";
    	s2 == "\0";
    
    	return 0;
    }

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i dont know why you're using a loop to simply compare two strings.btw in the if conditions you're checking for strcmp to be ==0 or >0 or <0.instead the returned value(value returned by strcmp should be checked).
    definition of strcmp(s1,s2) is
    compare string s1 to string s2;return <0 if s1<s2,0 if s1==s2,or >0 if s1>s2
    so your program should be like this.
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    int main() {
    
    	char s1[100];
    	char s2[100];
    	int x;
    	printf("Input first string:");
    	gets(s1);
    
    	printf("Input second string:");
    	gets(s2);
    
    	 x=strcmp(s1,s2);
    	if(x == 0)
    		printf("These strings are equal");
    	if(x > 0)
    		printf("The first string is larger");
    	if(x < 0)
    		printf("The second string is larger");
    	s1 == "\0";
    	s2 == "\0";
    	return 0;
    }
    also dont use gets for inputting strings instead use fgets.see faq.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    Ok but it stills does this weird thing where it doesnt show me what I want cuz it does stuff like the matrix.

  4. #4
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by jturner38 View Post
    Ok but it stills does this weird thing where it doesnt show me what I want cuz it does stuff like the matrix.
    You have the look of a man who accepts what he sees because he is expecting to wake up. Ironically, that's not far from the truth. - Morpheus


    remove the while loop and change your code to like this
    Code:
    //remove this	while(s1 != NULL){
    //remove this		 strcmp(s1,s2);
    	if((strcmp(s1,s2)) == 0)
    		printf("These strings are equal");
    	if((strcmp(s1,s2)) > 0)
    		printf("The first string is larger");
    	if((strcmp(s1,s2)) < 0)
    		printf("The second string is larger");
    }
    ...and you will have defeated the matrix

    and never use gets() function
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by creeping death
    remove the while loop and change your code to like this
    Refer to BEN10's post #2 for a better example. There is no point calling strcmp() thrice, especially when it runs in time proportional to the length of the shorter of the strings involved. I would further change it to:
    Code:
    x = strcmp(s1, s2);
    if (x == 0)
        printf("These strings are equal");
    else if (x > 0)
        printf("The first string is larger");
    else
        printf("The second string is larger");
    Additionally, these two lines have no net effect:
    Code:
    s1 == "\0";
    s2 == "\0";
    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

  6. #6
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    Quote Originally Posted by laserlight View Post
    Refer to BEN10's post #2 for a better example. There is no point calling strcmp() thrice, especially when it runs in time proportional to the length of the shorter of the strings involved. I would further change it to:
    Code:
    x = strcmp(s1, s2);
    if (x == 0)
        printf("These strings are equal");
    else if (x > 0)
        printf("The first string is larger");
    else
        printf("The second string is larger");
    Additionally, these two lines have no net effect:
    Code:
    s1 == "\0";
    s2 == "\0";
    i woundn't be too worried about both of them...let the compiler perform common subexpression elimination and dead code elimination...which all standard compilers do by default.
    Code:
    printf("%c%c%c%c%c%c%c",0x68,0x68^0xd,0x68|0x4,0x68|0x4,0x68|0xf,0x68^0x49,0x68^0x62);

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by creeping death
    i woundn't be too worried about both of them...let the compiler perform common subexpression elimination and dead code elimination...which all standard compilers do by default.
    Looking at the assembly output for a relevant sample test program compiled with the MinGW port of g++ 3.4.5 for both -O2 and -O3 optimisation levels, I'd say that the compiler has not reduced the calls of strcmp() to just one call, perhaps because it cannot prove that the calls will always return the same value.

    But optimisation aside, there is a maintenance benefit to moving the function call to before the if-else chain: you only need to change the function call in one place. Likewise, there is a maintenance benefit to removing the code that has no net effect: you, and future maintainers of the code, no longer have to ask why the code is there. After all, maybe it is actually a bug that never materialised, and a helpful maintainer might just introduce the bug while trying to "fix" the non-problem. Or maybe it is not dead code after all: rather, it is a bug because the code is supposed to have a net effect.
    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

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by creeping death View Post
    i woundn't be too worried about both of them...let the compiler perform common subexpression elimination and dead code elimination...which all standard compilers do by default.
    How could compiler know that strings are not changed between strcmp calls?
    for example by another thread?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Feb 2009
    Location
    Indiana
    Posts
    99
    This is what my program looks like now! It works but still does that funky matrix thing. Im tryin to figure out what is causing this.

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    int main() {
    
    	char s1[100];
    	char s2[100];
    	int x;
    	
    	printf("Input first string:");
    	
    
    	printf("Input second string:");
    	
    	x = strcmp(s1, s2);
    
    	if (x == 0)
    		printf("These strings are equal");
    	else if (x > 0)
    		printf("The first string is larger");
    	else
    		printf("The second string is larger");
    
    	s1 == "\0";
    	s2 == "\0";
    
    	return 0;
    }

  10. #10
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    What funky Matrix thing? Your code doesn't even get input from the user now! In fact I would guess that the output was something like this:

    Code:
    Input first string:Input second string:The second string is larger
    Not very Matrix like.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    lol, here is the light Neo:

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    
    #define MAX 100
    
    int main() {
    
    	char s1[MAX] = {0};
    	char s2[MAX] = {0};
    	int x;
    	
    	printf("Input first string:\n");
    	fgets (s1 , MAX , stdin);
    
    	printf("Input second string:\n");
    	fgets (s2 , MAX , stdin);
    
    	x = strcmp(s1, s2);
    
    	if (x == 0)
    		printf("These strings are equal\n");
    	else if (x > 0)
    		printf("The first string is larger\n");
    	else
    		printf("The second string is larger\n");
    
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    129
    J, whenever you see strange output you should copy it from the terminal to your post rather than try to describe it... A picture paints a thousand words; while we aren't talking about pictures, the same principle applies here.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by Jesdisciple View Post
    J, whenever you see strange output you should copy it from the terminal to your post rather than try to describe it... A picture paints a thousand words; while we aren't talking about pictures, the same principle applies here.
    I agree, it would be better if you could provide us the output that you're having for the people around to see what's actually happening.

    btw, fgets is also including the newline in your string so it would be better if you remove it and replace it with a nul terminator.

    Ex.
    Code:
    fgets (s1 , MAX , stdin);
    
    s1[ strlen( s1 ) - 1 ] = '\0';
    And I don't think that you can initialize an array in C like that...
    Code:
    	char s1[MAX] = {0};
    	char s2[MAX] = {0};

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by $l4xklynx
    btw, fgets is also including the newline in your string so it would be better if you remove it and replace it with a nul terminator.
    Yes, but the newline would not be present if the input fills the entire array (save for the last char). As such, it would be better to use strchr(), e.g.,
    Code:
    if (fgets(s1, MAX, stdin))
    {
        char *newline = strchr(s1, '\n');
        if (newline)
        {
            *newline = '\0';
        }
    }
    Quote Originally Posted by $l4xklynx
    And I don't think that you can initialize an array in C like that...
    Yes, you can, but in this case it would be slightly more descriptive to write:
    Code:
    char s1[MAX] = "";
    char s2[MAX] = "";
    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

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by laserlight View Post
    Code:
    if (fgets(s1, MAX, stdin))
    {
        char *newline = strchr(s1, '\n');
        if (newline)
        {
            *newline = '\0';
        }
    }
    I see, but why put fgets as an expression in the if condition?

    Quote Originally Posted by laserlight View Post
    Yes, you can, but in this case it would be slightly more descriptive to write:
    Code:
    char s1[MAX] = "";
    char s2[MAX] = "";
    yes they can but it's wrong in C, that's what I meant... But in his program I don't think that he needs to initialize the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM