Comparing Characters

This is a discussion on Comparing Characters within the C++ Programming forums, part of the General Programming Boards category; I was wondering how I could compare characters. For example, lets say I was trying to compare cLetter with cAnotherLetter ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    Comparing Characters

    I was wondering how I could compare characters. For example, lets say I was trying to compare cLetter with cAnotherLetter and check to see if they were equal.

    I tried to do this

    Code:
    main()
    {
     cLetter = "a";
     cAnotherLetter = "a";
    
     if (cLetter == cAnotherLetter)
     {
      cout << "Comparing letters, they are equal";
     }
    
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    51

    Continuation (Sorry I forgot to type the rest after I typed the code)

    I tried that, but I got errors. What would I need to get the correct check?

    Thanks in advance.

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    your close, you just need to use char's instead of c-strings

    like this...

    Code:
    main()
    {
     char cLetter = 'a';                // notice the single quotes
     char cAnotherLetter = 'a';
    
     if (cLetter == cAnotherLetter)
     {
      cout << "Comparing letters, they are equal";
     }
    
     return 0;
    }

  4. #4
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Well looks like things are already under control here

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Always put return types on your functions even though C++ automatically puts it to an int:
    Code:
    int main()
    {
       /* ... */
       return 0;
    }

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Originally posted by Speedy5
    Always put return types on your functions even though C++ automatically puts it to an int:
    Code:
    int main()
    {
       /* ... */
       return 0;
    }
    good call, i copied and pasted the posted code above when i replied. i didnt catch that one!

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    51
    Wow, thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 04:45 PM
  3. Comparing characters
    By Banana Man in forum C++ Programming
    Replies: 28
    Last Post: 01-13-2008, 11:11 PM
  4. Comparing characters
    By Thuz in forum C Programming
    Replies: 2
    Last Post: 09-16-2007, 01:07 PM
  5. Comparing characters.
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2001, 09:50 AM

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