Thread: a little help!

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Unhappy a little help!

    i'm new to programming and i was trying to compile this code:

    usage: as.exe value1 value2 value3
    Code:
    if(argv[2]=="x"){
    printf("worked\n");
    }else{printf("not worked\n");}
    but it always show me it worked!
    =P

    does any one can help me on this?

    thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by jonathanrs View Post
    i'm new to programming and i was trying to compile this code:

    usage: as.exe value1 value2 value3
    Code:
    if(argv[2]=="x"){
    printf("worked\n");
    }else{printf("not worked\n");}
    but it always show me it worked!
    =P

    does any one can help me on this?

    thanks
    You need to use strcmp to compare strings. Even so, the test should have failed since argv[2] most certainly does not have the same address as the string literal.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    What Sebastiani stated, but on a tangent you may want to review the use of getopt().

    Example of Getopt - The GNU C Library

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    thanks

    sorry people! something really weird happened to this computer while trying to compile this code, or it just didn't want to follow my commands. =P

    i'd tried everything and this:

    Code:
    if(strcmp(argv[2],"x")==FALSE) {
    printf("worked");}else{printf("error");}
    always returned true! but today i've compiled this code again and it worked! o0

    thanks ^^

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> always returned true! but today i've compiled this code again and it worked! o0

    Post the entire program. And strcmp doesn't return false/true. Always be sure to read the documentation before attempting to use any function/library.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    sorry

    Code:
    main()
    {
    char var[8]="abcdefgh";
    
    if(strcmp(var,"abcdefgh")==1){printf("true\n");
    system("pause");
    }else{printf("false\n");system("pause");}
    
    }
    When i said it always returned true, i was talking about 1 and 0.The function returns an int. That's the code. If i change the size of var,for ex. var[10]="abcdefgh", the strcmp() function will give me printf("false");

    thanks

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Um, strcmp() returns negative integer, zero (0), or a positive integer depending on how its two arguments compare with each other... i.e. A < B, A ==B, or A > B respectively.

    The way you've coded it, you are interpreting a +1 as "true". When A > B you could get any positive integer - not necessarily limited to +1.
    Perhaps you are getting your "false" because the strings are indeed equal.

    You should probably allow for the null terminator - so do var[9]. I'm surprised the compiler didn't whine.
    Last edited by nonoob; 08-01-2009 at 03:14 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's actually valid to put a string without a string literal into an array in C, so it's no wonder it doesn't complain.
    However, you must ALWAYS null terminate strings if you want to use string functions with it. Don't specify the size; instead, let the compiler figure it out:
    Code:
    char str[] = "My string";
    Also, main must return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Tags for this Thread