Thread: IF problems

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    IF problems

    Code:
    if(input == "pwd")
    {
    	system("cd");
    }
    else
    {
    	system(input);
    }
    For some reason, when I enter in pwd, it tries to execute it instead of executing "cd", like I told it to. What is going on here?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( strcmp( foo, bar ) == 0 )
        printf( "We have a match!" );
    else
        printf( "Variables 'foo' and 'bar' are different." );
    There you go.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    just like quzah said.

    you need to use strcmp from the <string.h> library to compare strings.

    if strcmp returns 0 then they are a match
    if strcmp returns 1 then first arg greater then second arg
    if strcmp returns -1 then first arg is less then second arg

    cheers
    there are only 10 people in the world, those who know binary and those who dont

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    So...
    Code:
    if(strcmp(input,"pwd") == 0)
    {
    	system("cd");
    }
    else
    {
    	system(input);
    }

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    the if always fails?

    are you sure you getting "pwd" back and not "pwd\n"?

  6. #6
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    YES!

    Thank you very much, mart_man! The program works now!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You could always read the FAQ . It'll answer some of your questions for you...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM