Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am to read a csv file into my structures and vectors and sort it according to the columns...from heatNo, distance and time.
The problem with my code is that after running, my vector remains empty and if i don't know how to cout the data after sorting

What I have tried:

C++
#include <fstream>
#include <iostream>
#include <string>
#include <sstream> //stringstream
#include <vector>
#include <algorithm> //for sort()
using namespace std;

struct Details{
	string name;
	string dob;
	int heatNo;
	int distance;
	double time;
	string status;
};
bool multipleFields(const Details &lhs, const Details &rhs)
{
	if (lhs.heatNo < rhs.heatNo)
		return true;
	else if (lhs.distance < rhs.distance)
		return true;
	else if (lhs.time < rhs.time)
		return true;
}
int main()
{
	vector <details> results;
	ifstream inputFile ("C:\\Users\\mitch\\Desktop\\Data Structures\\projectInput.csv");
	Details swimmer;
	string line;
	
	while (!inputFile.eof() && getline(inputFile, line, '|'))
	{
		cout << line << "\t";
		stringstream ss(line);
		Details swimmer;
		for (int i = 0; i < 1e4; i++)
		{
			if (ss >> swimmer.name >> swimmer.dob >> swimmer.heatNo >> swimmer.distance >> swimmer.time >> swimmer.status)
			{
				results.push_back(swimmer);
			}
		}
	}
	inputFile.close();  //closes the file 
	cout << results.size();
	sort(results.begin(), results.end(), multipleFields); 
	system("pause");
	return 0;
}
Posted
Updated 8-Feb-18 21:27pm
v2

1 solution

Quote:
vector
results;
The above line should prevent correct compilation of your code.

If your vector is empty after execution of the while loop then something went wrong either in the while control expression or in (all) the stringstream extraction operator calls (why so many calls?). You may either debug or log what happens.
 
Share this answer
 
Comments
Member 13669380 9-Feb-18 9:41am    
I need the calls to output each data. I think it's the stringstream giving me problems. How can I remove it and put in place a for loop. I have tried different ways but i get errors in VS

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900