argv and atoi

This is a discussion on argv and atoi within the C Programming forums, part of the General Programming Boards category; Code: int main(int argc, char *argv[]) { if (!atoi(argv1]) >= 1 || !atoi(argv[1]) <= 4) { printf("Blah"); } return 0; ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    argv and atoi

    Code:
    int main(int argc, char *argv[])
    {
            if (!atoi(argv1]) >= 1 || !atoi(argv[1]) <= 4)
    	{
                  printf("Blah");
    	}
    
           return 0;
    }

    usage: crap.exe 1

    i just get "Blah" no matter what i enter???

  2. #2
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Code:
    if (!atoi(argv1])
    zwha?

    hint : that "!" negates everything (non zero becomes zero, zero becomes 1)



    and yea... even without "!"... ur code will obviously print "blah" whatever the input is... because every number is either bigger than 1 OR less than 4
    Last edited by yxunomei; 10-14-2006 at 05:25 AM.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm this won't even work:

    Code:
    atoi(argv[1]) != 1 || atoi(argv[1]) != 2
    i can enter any number and it just says blah as soon as i take out the || it works fine ?? :|

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    {
        if (atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4)
           printf("Blah");
    	
        getchar();
        return 0;
    }
    ssharish2005

  5. #5
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    hmm that still doesn't work. i want to print blah if the number provided is not between 1 and 4.

  6. #6
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    {
        if (atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4)
           printf("Blah");
    	
        return 0;
    }
    This prints whenever you put 1 to 4 inclusive, and now you want the other way around rite?



    so.... enuff hint?
    Last edited by yxunomei; 10-14-2006 at 05:46 AM.

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by Axel
    hmm this won't even work:

    Code:
    atoi(argv[1]) != 1 || atoi(argv[1]) != 2
    i can enter any number and it just says blah as soon as i take out the || it works fine ?? :|
    That's the logic! Whatever you enter, the statement will return true.
    Quote Originally Posted by yxunomei
    This prints whenever you put 1 to 4 inclusive, and now you want the other way around rite?

    so.... enuff hint?
    Wow, you managed to copy ssharish2005's code.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by yxunomei
    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    {
        if (atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4)
           printf("Blah");
    	
        return 0;
    }
    This prints whenever you put 1 to 4 inclusive, and now you want the other way around rite?

    so.... enuff hint?
    heh.. it isn't much different to what i had originally..anyway i got it (!(....

  9. #9
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    it's MUCH different... u used " || " whereas it uses " && " and also, it doesn't have that " ! " thingy...
    but if you are talking about de morgan's law... yes, i'd say it's not much different

    consider these
    Code:
    I am going to the beach and (&&) the movie
    I am going to the beach or (||) the movie
    but "or" in daily english conversation refers more to (^) XOR

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    #include<stdio.h>
    
    int main(int argc, char *argv[])
    {
        if (atoi(argv[1]) >= 1 && !(atoi(argv[1]) <= 4))
           printf("Blah");
    	
        getchar();
        return 0;
    }
    ssharish2005

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,530
    how about
    Code:
    int num = (int) strtol(argv[1], NULL, 10);
    if ( num < 0 || num > 5)
       printf("Blah");
    Clearly the other suggestions won't work.
    Code:
    Owner@pavilion ~
    $ cat a.c
    #include <stdio.h>
    
    #define WYSIWYG(x) #x " = %d\n", x
    
    int main (void)
    {
       printf( WYSIWYG(!1 >= 4 || !1 <= 4) );
       printf( WYSIWYG(!2 >= 4 || !2 <= 4) );
       printf( WYSIWYG(!3 >= 4 || !3 <= 4) );
       printf( WYSIWYG(!4 >= 4 || !4 <= 4) );
       puts("");
       printf( WYSIWYG(1 >= 4 && 1 <= 4) );
       printf( WYSIWYG(2 >= 4 && 2 <= 4) );
       printf( WYSIWYG(3 >= 4 && 3 <= 4) );
       printf( WYSIWYG(4 >= 4 && 4 <= 4) );
       return 0;
    }
    Owner@pavilion ~
    $ ./a
    !1 >= 4 || !1 <= 4 = 1 /* True always */
    !2 >= 4 || !2 <= 4 = 1
    !3 >= 4 || !3 <= 4 = 1
    !4 >= 4 || !4 <= 4 = 1

    1 >= 4 && 1 <= 4 = 0 /* backwards apparently */
    2 >= 4 && 2 <= 4 = 0
    3 >= 4 && 3 <= 4 = 0
    4 >= 4 && 4 <= 4 = 1

    That's a fun widget...
    Last edited by whiteflags; 10-14-2006 at 05:57 AM.

  12. #12
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about

    Code:
    if (!(atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4))
    Won't that work ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  13. #13
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    hmmm... i havn't tested it, but if i'm not mistaken, this
    Code:
    if (!(atoi(argv[1]) ...
    will convert anything non zero to zero, and zero is always less than 1, then it will always be false
    Code:
    if (0 >= 1 ...
    CMIIW

  14. #14
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You're reading that wrong. It converts everything that's greater or equal to one AND smaller or equal to four to false.

    Maybe I should space out my parentheses a little more :

    Code:
    if ( !(  atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4  )  )
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  15. #15
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Happy_Reaper
    How about

    Code:
    if (!(atoi(argv[1]) >= 1 && atoi(argv[1]) <= 4))
    Won't that work ?
    well, OP wanted to print blah when it is more than 5 and nothing should be printed when its is between 1 and 4.

    ssharish2005

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3x syntax errors :S, Help a student finalise an assignment
    By Chickenhawk in forum C Programming
    Replies: 14
    Last Post: 07-27-2006, 05:14 AM

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