Thread: comparing character in a string to anothr character

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    5

    comparing character in a string to anothr character

    I wanted to use:
    Code:
    if((*(number+i)=='\n'))
    number being declared as char number[15].
    It doesn't work though and I can't understand why.
    Using:
    Code:
    if((*(number+i)-'\n'))
    works but it doesn't do what I need.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if((*(number+i)=='\n'))
    If this is true

    > if((*(number+i)-'\n'))
    Then this will be false (the result of the expression being zero).
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    I probably should have explained better. What I meant with "it doesn't work" was that when I simply run it, then it won't go any further than this check, but it doesn't hang either.
    When I compile it for debugging in DevC++ and then run step-by-step then pressing next step does nothing. It gets stuck for some reason.

  4. #4
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    My guess is that the address (number+i) may be going out of bounds, ensure that (i >=0 && i < 15)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post more code, like the whole function, and a 1-line main which calls that function with whatever parameters make it fail.

    If the function reads user input, then comment out those lines and replace with data,

    so
    scanf("&#37;s",input);

    becomes
    char input[100] = "hello";
    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.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    5
    Quote Originally Posted by @nthony View Post
    My guess is that the address (number+i) may be going out of bounds, ensure that (i >=0 && i < 15)
    That's it! I had i++ before do-while while statement that checked for *(number+i)!='\n' but whenever the loop reached \n it replaced it with \0 then incremented i and checked whether next character in a string is \n. But there is only one \n in that string. Seems I was dealing with infinite loop .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM