i am trying to read the following information:

Marsfield,North Ryde,2.4
North Ryde,Epping,5
Carlingford,North Ryde, 3.5
Carlingford,Marsfield, 4.7
North Rocks,Epping,4.8
Carlingford,North Rocks,2.4
North Ryde,Ryde,2.1

so i will be able to store the information into a 2 strings and a float (obviously excluding the comma. eg:

string station1 = North Ryde
string station2 = Ryde
float distance = 2.1

this is what i have so far...

void readText (string fileName)
    string station1;
    string station2;
    string distance;
    ifstream infile;
    if (!infile)
         cout << " File not found";
    while(!infile.eof()) // To get you all the lines.
         getline(infile, station1, ',');
         getline(infile, station2, ',');
         getline(infile, distance);
         cout << station1 << " ---> " << station2 << endl;
         cout << "Distance = " << endl;
         cout << endl;

and this is the output i am getting...

Enter filename : suburb.txt
Carlingford ---> Epping
Distance =

Epping ---> Marsfield
Distance =

Marsfield ---> North Ryde
Distance =

North Ryde ---> Epping
Distance =

Carlingford ---> North Ryde
Distance =

Carlingford ---> Marsfield
Distance =

North Rocks ---> Epping
Distance =

Carlingford ---> North Rocks
Distance =

North Ryde ---> Ryde
Distance =

 ---> Ryde
Distance =

Press any key to continue . . .

i am not really sure what to try next and have gone through dozens of different sample code, i am also unsure why i am getting the extra value at the end with no station1 value.

any help would be much appreciated!

Edited by timb89: n/a

6 Years
Discussion Span
Last Post by timb89

Your distance is not being shown on the screen because you don't include it in the cout statment.

An extra newline character in your text file is the culprit. Put the cursor at the end of the last line and hit delete (not backspace). I had thought that it was !infile.eof() that was causing the repeat. However, using .eof() is still a bad idea (see this http://www.daniweb.com/forums/post155265-18.html -- RIP Dave). Use the first getline statement to "drive" the loop.


Also, you haven't converted your distance into the float variable, it is still a string.

Edited by jonsca: n/a

Votes + Comments
Catch sight of!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.