Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Problem with reading from file to a vector of struct

$
0
0
Hi!

I'm trying to read in a list from a file into a vector of struct. It reads in the first row normally and then like this:

Posted Image

and it reads the last row two times. Don't really know why. Thanks for any help!

The original file looks like this:

John|Smith|johsmi01|1.67|
Hanna|Barbera|hanbar01|1.45|
Johanna|Smith|johsmi02|1.56|
Bob|Taylor|bobtay01|1.76|

void loadList(vector <Person> &persons)
{
	string filename;
	Person newPerson;
	cout << "Reading list from a text file" << endl;
	cout << "\nType in the filename you want to open: " << endl;
	cin >> filename;

	ifstream inFile(filename.c_str());
	persons.clear();

	if(inFile.is_open())
	{
		do
		{   
			getline(inFile, newPerson.firstName, DELIM);
			getline(inFile, newPerson.lastName, DELIM);
			getline(inFile, newPerson.sign, DELIM);
			inFile >> newPerson.height;
			inFile.get();
			persons.push_back(newPerson);
		}while(!inFile.eof());

		inFile.close();
		cout << "The file has opened!" << endl;
	}
	else
	{
		cout << "The file couldn't be opened!" << endl;
	}
};


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>