I am having a problem with casting

This is a discussion on I am having a problem with casting within the C++ Programming forums, part of the General Programming Boards category; Here is the code that is having the problem int questions() { while(strchr(uresponse,"?")==0) { cout << "I cannot yet respond ...

  1. #1
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60

    I am having a problem with casting

    Here is the code that is having the problem

    int questions()
    {
    while(strchr(uresponse,"?")==0)
    {
    cout << "I cannot yet respond to questions.\n";

    }
    }

    and the error I get is this:
    passing `const char *' to argument 2 of `strchr(const char *, int)' lacks a cast

    I was reading in the programmers bible about casting and I cant exactly figure this one out. Can anybody be so nice as to help?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You don't need a cast. Try -

    strchr(uresponse,'?')

  3. #3
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60
    thank you very much, that worked fine. What is the difference in "?" and '?'? I have used quotations when searching for full words within a given phrase and that works fine.

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    One's a char literal , the others a string literal -

    '?' == {'?'}
    "?" == {'?','\0'}

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    ...and because the compiler cannot implicitly convert a char* (the "?") into the int the function expects, but can convert a char (the '?') into an int since a char is essentially an integral type just like ints are.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. type casting problem?
    By lackofcolour in forum C Programming
    Replies: 6
    Last Post: 01-30-2006, 03:29 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

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