Thread: Checking for ' in an if

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Checking for ' in an if

    Hello all;

    Having a brain fart here.

    I want to check if a char is == to an ' or not.


    the statement if(char == ''') does not work bc the compiler sees the second ' as the end and the third as erroneous. Sooo....

    how can I test for this condition

  2. #2
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Escape sequences.

    Use the escape sequence for a single quote ( \' ) inside your quotes.

    Code:
    if (x == '\'')
    ...
    Last edited by Decrypt; 06-30-2006 at 07:33 AM.
    There is a difference between tedious and difficult.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    escape with a backslash, e.g. '\''
    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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Use a \
    Code:
    '\''
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  4. wont write to variable
    By Klinerr1 in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:00 PM
  5. off the screen
    By Klinerr1 in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2002, 08:47 PM