Thread: Getline Ignores SOME whitespaces

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    20

    Getline Ignores SOME whitespaces

    I'm using getline to read in from a file line by line but it skips the first two whitespaces.

    Example input:
    1 0.7 mountain and grant

    How it's being stored:
    10.7mountain and grant

    Thanks for your help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Showing us the code would help.

    Like for example, what reads the first two numbers?
    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 2012
    Posts
    20
    Here is the code.
    Code:
    while(!inFile.eof()){
    		getline(inFile, inputLine);	
    		
    		if(inFile.good()){
    			if(inputLine == "INTERSECTIONS:" || inputLine == "STREETS:"){
    				continue;
    			}
    			istringstream lineStream(inputLine);
    			lineStream >> temp >> safety >> intersect1 >> intersect2 >> intersect3;
    all the variables are the correct data types.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    You can simplify:
    Code:
    while (getline(inFile, inputLine)) {
        if(inputLine == "INTERSECTIONS:" || inputLine == "STREETS:") {
            continue;
        }
        istringstream lineStream(inputLine);
        lineStream >> temp >> safety >> intersect1 >> intersect2 >> intersect3;
    Anyway, you probably don't have a problem besides having printing the values without adding a space, so you just confused yourself.
    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

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    There's a tab between the 1 and 0.7 and between the 0.7 and mountain.

    Getline is ignoring the tabs. Is there a way so it will read it?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by calCOOLus View Post
    Getline is ignoring the tabs.
    This statement is false. Using >> extraction operators will discard whitespace, and as laserlight mentioned when you print it's your responsibility to include whitespace between fields.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    So how could I not discard the whitespace without using '>>'?
    Thanks.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by calCOOLus View Post
    So how could I not discard the whitespace without using '>>'?
    Thanks.
    The actual question is: why do you want it? (Hint: you don't.) It does not belong in any of your variables -- it's just the delimiter between them.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    I guess then is how do I store the values and strings separately?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by calCOOLus View Post
    I guess then is how do I store the values and strings separately?
    You've had that the whole time. temp is 1. safety is 0.7. You don't want either of these values to have a tab in them, because, well, they're numbers and they don't have tabs. Meanwhile, intersect1 is "mountain", intersect2 is "and", and intersect3 is "grand". Again (unless you were trying for all of that in one variable) the spaces don't "belong" in your data.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    Yeah but when I read from the file using getline(inFile, inputLine) it combines 1 0.7 and mountain so inputLine = 10.7mountain and grant.
    Sorry if I'm not understanding correctly.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming your file does contain tabs, then inputLine contains "1 0.7 mountain and grant" (I have taken the liberty of showing tabs as spaces, although technically they're usually shown as \t). It does not contain "10.7mountain and grant".

    Those pieces then get extracted into your variables when you do >>, and your tabs will disappear at that time.

    (EDIT TO ADD: If you print inputLine as itself (as opposed to using the pieces temp and safety and the rest), you will see the tabs appear.)
    Last edited by tabstop; 12-11-2013 at 07:25 PM.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    So this should be my final question, why is my program breaking after
    "lineStream >> temp >> safety >> intersect1 >> intersect2 >> intersect3;"


  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As posted, that's the last line of your program. If there are lines after that, then I know noooooooothing.

  15. #15
    Registered User
    Join Date
    Nov 2012
    Posts
    20
    I should have said it breaks at that line. After that line I'm just pushbacking the variables into a vector.

    Edit: It was breaking at that line because intersects1 2 & 3 were char* instead of strings.

    Thanks for all your help and bearing with me.
    Last edited by calCOOLus; 12-11-2013 at 08:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code ignores IF Statement
    By Neil Naik in forum C Programming
    Replies: 12
    Last Post: 07-02-2012, 07:57 PM
  2. do-while loop ignores break
    By edsoneicc in forum C Programming
    Replies: 8
    Last Post: 02-28-2012, 11:19 AM
  3. getchar ignores EOF
    By inliner in forum C Programming
    Replies: 9
    Last Post: 10-21-2010, 11:40 AM
  4. do - while loop ignores getchar()
    By TheIggle in forum C Programming
    Replies: 3
    Last Post: 07-15-2010, 07:51 AM
  5. Replies: 7
    Last Post: 05-23-2010, 07:10 AM

Tags for this Thread