Thread: invalid comparison

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    invalid comparison

    Hey, I am getting a compiler error that I don't agree with (haha). I am attempting to compare chars, but the error the compiler is giving me is:

    Code:
    46 prog6.cpp ISO C++ forbids comparison between pointer and integer
    My code is:

    Code:
    int main(int argc, char *argv[])
    {
    
    [snip]
    
    if(argv[typeIndex] != 's')
        {
            cerr << "Invalid Type argument '" << argv[typeIndex] << "'.\n";
            exit(1);
        }
    
    }
    If I changed the single quotes around the s to double quotes "s" it compilers, however even if argv[typeIndex] is equal to "s" the error message still appears.

    Code:
    Invalid Type argument "s".
    Any ideas?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Well I think I just solved my problem, by changing it to:

    Code:
    if(*argv[typeIndex] != 's')
    However, when I try to add to this statement, I get an error, like above when I run the program - even if the argument is 's', it still spits out the error:

    Code:
    if((*argv[typeIndex] != 's') || (*argv[typeIndex] != 'b') || (*argv[typeIndex] != 'q'))
        {
            cerr << "Invalid Type argument '" << argv[typeIndex] << "'.\n";
            exit(1);
        }
    It works fine without the OR statements (just the checking for 's')
    Last edited by Beowolf; 12-02-2007 at 03:40 PM.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    char *argv[] is the same as char** argv, so what you did is argv[index] == char* and 's' == char, hence the compile error. Both types must be the same types.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    argv is an array of strings (char pointers), not of chars.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    If I changed the single quotes around the s to double quotes "s" it compilers, however even if argv[typeIndex] is equal to "s" the error message still appears.
    use strcmp to compare strings. What you are doing now is comparing pointers (whether they point to the same address, and they never will)

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Any idea why when I put OR statements inside the working if statement that it then no longer works?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Also, any idea as to why I am getting this error?

    Code:
    78 Cprog6.cpp [Warning] comparison is always false due to limited range of data type
    Code:
    if(*argv[countIndex] > 32767)
        {
            cerr << "Invalid count argument '" << argv[countIndex] << "'.\n";
            exit(1); 
        }
    countIndex would be a value like 4 or 5. If I do (int)argv[countIndex] instead of *, it says that the value is like 53 when argv[countIndex] is actually 32000. I don't understand what is going on.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess, you are using a DOS based compiler, e.g. Turbo C?

    In that case, 32767 is the highest integer value you could get to - so comparing with that will ALWAYS BE FALSE - so it's not much point to make the comparison and have the code inside the if-statement. If you add one, you should get 32768, but it is a negative nunber [-32768] because signed integers use the highest bit to indicate sign.

    I seem to remember that Turbo & Borland C/C++ compilers give that error message.

    You get the same if you try to comare an unsigned value for "less than zero", as that's also impossible to achieve.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Ah, thanks. Another logic problem that I am stuck on. I have this code:

    Code:
        cout << "\t[" << *argv[seedIndex] << "]\n";
    
        if(*argv[seedIndex] < 1)
        {
            cerr << "Invalid Seed argument '" << argv[seedIndex] << "'.\n";
            exit(1);
        }
    I run the program so it prints out
    Code:
    [0]
    for argv[seedIndex], yet my error message never gets executed - it should, because 0 < 1, but it doesn't, and I don't know why :\
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if(*argv[countIndex] > 32767)
    Isn't *argv[countIndex] the same as argv[countIndex][0]? In which case you would be comparing a character with a large integer. (A character can have the value of 255 or 127 at most, so any char is always less than 32767.)

    I run the program so it prints out
    [0]
    You are printing the ascii character '0' which has an integral value of 48 (or so). Which is very different from character '\0' (integral value 0), which is a non-printing character (you might see a space or something like that).

    Edit: Commandline arguments are char arrays. You cannot treat them as integers. If you want to interpret the arguments as integers, you'll first need to convert them into ints, using std::stringstream's or C conversion functions.
    Last edited by anon; 12-02-2007 at 05:26 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Regarding your OR-chain, write out what you're testing and think about it:

    If the character is not 's' OR the character is not 'b' OR the character is not 'q'
    then print an error message.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Flood of errors when include .h
    By erik2004 in forum C++ Programming
    Replies: 14
    Last Post: 12-07-2002, 07:37 AM