Comparing strings

This is a discussion on Comparing strings within the C++ Programming forums, part of the General Programming Boards category; Does anyone know how to compare string[i] with a letter, say "x" in a for loop? I've tried to do ...

  1. #1
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870

    Comparing strings

    Does anyone know how to compare string[i] with a letter, say "x" in a for loop? I've tried to do this:
    Code:
    if (string[i] == "x"){}
    But my compiler says: ISO C++ forbids comparison between pointer and integer. i is defined as int i = 0 and string is a C++ style string. Any help would be nice.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Single characters go between single quotes.

    Code:
    if (string[i] == 'x') {}

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Of course! Thank you some much, such a simple mistake!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. comparing strings
    By infinitum in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2003, 12:10 PM
  5. Comparing Strings
    By Perica in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2003, 10:41 PM

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