Thread: Newline character not being picked up in a conditional statement

  1. #1
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181

    Newline character not being picked up in a conditional statement

    I have the following code:

    Code:
                while (lineread != "\n")
                {
                    getline(ifs,lineread);
                }
    In essence, it reads an input file, line by line until it hits a line only containing a newline character. So if the input text file is:

    2 7
    GC 0 1 2 3 4 5 6
    1 0 290 590 880 1170 - -
    2 0 340 680 1020 1360
    3 0 390 770 1160 1400
    4 0 430 860 1290 1400
    5 0 470 940 1400
    6 0 510 1010 1400

    7 12.5
    GC 0 1 2 3 4 5 6
    1 0 340 690 1030 1380 - -
    2 0 410 820 1230 1630
    3 0 470 940 1410 1880
    4 0 530 1060 1590 2120
    5 0 590 1180 1770 2360
    6 0 650 1290 1940 2500
    It should exit the while loop after hitting line 6. What happens however is that it goes into a perpetual loop and doesnt exit the while loop upon reading line 6.

    What I did was changed the code to:
    Code:
                while (lineread != "*")
                {
                    getline(ifs,lineread);
                }
    and the input file to:
    2 7
    GC 0 1 2 3 4 5 6
    1 0 290 590 880 1170 - -
    2 0 340 680 1020 1360
    3 0 390 770 1160 1400
    4 0 430 860 1290 1400
    5 0 470 940 1400
    6 0 510 1010 1400
    *
    7 12.5
    GC 0 1 2 3 4 5 6
    1 0 340 690 1030 1380 - -
    2 0 410 820 1230 1630
    3 0 470 940 1410 1880
    4 0 530 1060 1590 2120
    5 0 590 1180 1770 2360
    6 0 650 1290 1940 2500
    and finally it stops after line 6. In this case, merely changing the "\n" conditional character to a simple asterisk character "*" fixes the problem.

    However I wish to keep the input text file as is with the newlines, so how do I make it exit the while loop when detecting a \n as a line?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,065
    The newline character is not stored, so your loop should be more like:
    Code:
    while (getline(ifs, lineread) && !lineread.empty())
    {
        // ...
    }
    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

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    15
    I think getline discards the delimiter (by default '\n') so you should be testing for "" instead of "\n".

  4. #4
    Registered User Vespasian's Avatar
    Join Date
    Aug 2011
    Posts
    181
    Quote Originally Posted by laserlight View Post
    The newline character is not stored, so your loop should be more like:
    Code:
    while (getline(ifs, lineread) && !lineread.empty())
    {
        // ...
    }
    Quote Originally Posted by dandrestor View Post
    I think getline discards the delimiter (by default '\n') so you should be testing for "" instead of "\n".
    Both solutions work. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-12-2010, 12:54 PM
  2. regarding conditional statement in xml
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 07-21-2007, 10:22 PM
  3. How to get rid of newline character
    By C++angel in forum C++ Programming
    Replies: 3
    Last Post: 02-07-2006, 07:50 PM
  4. Newline character
    By sean in forum Networking/Device Communication
    Replies: 6
    Last Post: 11-24-2004, 03:33 PM
  5. Character input and conditional processing
    By Yawgmoth in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2002, 06:25 PM