Results 1 to 6 of 6

Thread: runs all ifs??? [*resloved*]

  1. #1

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    runs all ifs??? [*resloved*]

    Code:
    if(argc>2)
    {
    if(argv[1]="open")
    	{
    		...
    	}
    	else if(argv[1]="new")
    	{
    		...
    	}
    }
    when i enter in "cmdline.exe open c:\test.txt", it runs fine, when i run £cmdline.exe new c:\newtext.txt" it trys to open c:\newfile.txt? is this because im trying to use a string in the "if(argv[1] = ..." part??



    Thanks
    Last edited by john tindell; Nov 5th, 2002 at 03:41 PM.

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    You are using the assignment operator in the if statement. You MEANT ==.

    Second, unless you converted your command line arguments to string objects in C++, you cant use the == operator to determine equality of the strings. Use the strcmp function instead.

    Z.

  3. #3
    Hyperactive Member TupacShakur's Avatar
    Join Date
    Mar 2002
    Location
    Da Land Of Da Heartless...
    Posts
    493

    Smile Well

    Maybe somethig like this:
    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main(int argc, char * argv[])
    {
    char * Str1 = "open";
    char * Str2 = "new";
    
    if(argc > 2)
    	{
    	if(strcmp(argv[1], Str1) == 0)
    		{
    		//...
    		}
    	else if(strcmp(argv[1], Str2) == 0)
    		{
    		//...
    		}
    	else
    		cout<<"Wrong usage."<<endl;
    	}
    else
    	cout<<"Wrong usage."<<endl;
    
    return 0;
    }
    Hope this helps.
    "And Now I'm Lika Major Threat, Cause I Remind U Of The Things U Were Made To Forget!" - (2PAC)

    "Now They Label Me a Lunatic, Couldn't Care Less, Death or Success is What I Quest, Cause I'm Fearless!" - (2PAC)

    " There's a light at the end of every tunnel, just pray it's not a train!! "



    I am 100% addicted to Tupac. What about you?
    I am 24% addicted to Counterstrike. What about you?
    The #1 Tupac Fans Web Site | The Official Tupac Web Site

  4. #4
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Yeah, though you dont have to store those strings before using them in the strcmp function... you can simply call it like so:
    Code:
    strcmp(argv[1], "new") == 0
    Z.

  5. #5

    Thread Starter
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    cheers thanks for the help

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    <iostream> not <iostream.h>.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width