i am trying to read the following information:

Carlingford,Epping,3
Epping,Marsfield,6.5
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;
    infile.open(fileName.c_str());
    
    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;
    }
    
    infile.close();
}

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!

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.

while(getline(infile,station1,',')
{
   getline(infile,station2,',')
   getline(infile,distance);
   //etc.
}

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

commented: Catch sight of! +9
commented: *nods* +11

I have edited the above post a bit (just so you get the new changes)

ah ok i see the problem thanks for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.