Thread: Checking to see if a string is just a newline character

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    Checking to see if a string is just a newline character

    Hey, I am trying to check and see if a user-inputed string contains just the newline character (i.e. a blank return). The code I have right now doesn't compile, and I believe that is because it would only work on a character, not a string:

    Code:
    while(getline(cin, searchWord) != NULL)
        {
            searchWord = modifyWord(searchWord);
            if(searchWord == '\n')
            {
                searchResult = tree.search(searchWord);
                if(searchResult != -1)
                    cout << searchWord << ": line " << searchResult << ".\n";
                else
                    cout << searchWord << ": not found.\n";
            }
        }
    I thought about checking the string size since the only case when that would be equal to 1 is when the only character in the string is the newline character, but I thought that that might bring up some problems also.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > if(searchWord == '\n')
    You might try:
    Code:
            if (searchWord == "\n")

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    That compiles but it doesn't work (still says ": not found").
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >That compiles but it doesn't work (still says ": not found").
    That would indicate searchWord isn't equal a newline then. getline() wouldn't normally contain a newline, unless you change the third parameter. Maybe it should actually be blank:
    Code:
            if (searchWord == "")
    Otherwise print out searchWord to see what it contains.

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. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM