correcting my last thread...

This is a discussion on correcting my last thread... within the C Programming forums, part of the General Programming Boards category; hi! I want it to let it sound a little more easier to understand! I recognized that my last question, ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    34

    Wink correcting my last thread...

    hi! I want it to let it sound a little more easier to understand!
    I recognized that my last question, 'My C-Compiler doesn't like else if' was a little bit... .Let's no longer talk about it ;-)!

    the code was something like this:

    Code:
    ...
    int main(int argc, char *argv[])
    {
    if (argc < 2 )
    //usage menue!
    
    else if (argv[1] = "Usageone")
    //usage one
    
    else if (argv[1] = "usagetwo")
    //usage two
    and so on...


    When I compiled it, there was no errors or warnings!
    When I startet it without arguments, the usage menue was shown like I thought it would happen, but then I was really ... scared about the next move: Checking the other arguments!
    if I tried "programm usageone", everything was Ok, if I tried "programm usagetwo" or "programm usagenothing", the programm showed me usageONE! Ok? THIS was realy not the thing I expected! I tried different things, but nothing worked!
    Can somebody help me now? Or is the answer the same like my last thread 'My C-Compiler doesn't like else if'? Answer please to demonus@gmx.net or this board!

    P.S.: This page is the best!

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) When you compare, use == not =

    2) You can't compare strings using ==, use strcmp();
    Code:
    int main(int ArgCount, char *Args[])
    {
       if(ArgCount > 1)
       {
          if(strcmp(Args[1], "usageone") == 0)
          {
             //Yada yada...
          }
       }
    }
    Be careful. strcmp is case sensitive, meaning "Hello" != "hello".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The reason why Usageone is always run is because of the condition, which is an assignment. You are trying to assign the string Usageone to argv[1]. An assignment evaluates to True, this condition is alwaysTrue.

    For string comparing, you need the function strcmp(), which returns 0 when two strings are equal. You'll need string.h.

    Code:
    #include <string.h>
    
    if (strcmp (string1, string2) == 0)
    {
        /* string1 equals string 2 */
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shiro
    An assignment evaluates to True, this condition is alwaysTrue.
    An assignment returns the value that is being assigned, not true or false (but since true = !false = !0, that is true in almost every case ). That's why you can nest assignments:

    X = Y = Z = 5;
    X = Y = (Z = 5);
    X = Y = 5;
    X = (Y = 5);
    X = 5;
    (X = 5);
    5;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  2. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 04:55 PM
  3. Win32 Thread Object Model Revisted
    By Codeplug in forum Windows Programming
    Replies: 5
    Last Post: 12-15-2004, 08:50 AM
  4. Simple thread object model (my first post)
    By Codeplug in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2004, 11:34 PM
  5. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM

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