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.
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.
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.
Here is the code.
all the variables are the correct data types.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;
You can simplify:
Anyway, you probably don't have a problem besides having printing the values without adding a space, so you just confused yourself.Code:while (getline(inFile, inputLine)) { if(inputLine == "INTERSECTIONS:" || inputLine == "STREETS:") { continue; } istringstream lineStream(inputLine); lineStream >> temp >> safety >> intersect1 >> intersect2 >> intersect3;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
So how could I not discard the whitespace without using '>>'?
Thanks.
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.
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.
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.
So this should be my final question, why is my program breaking after
"lineStream >> temp >> safety >> intersect1 >> intersect2 >> intersect3;"
As posted, that's the last line of your program. If there are lines after that, then I know noooooooothing.
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.