Sorting Names With Commas From File

I'm having trouble trying to importing the names from my text file and displaying them correctly.
Every time I do, they display in the order, but they display in this order

Example
1
2
3
4
5
Last Name,
First Name
Last Name, 
First Name



and I would Like for it to display

 
Last Name, First Name


So I Could Then Sort it By Last Name In Order

This is the Code I currently Have


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
	
	vector<string> nameList;
	ifstream readfile("UnsortedNames.txt");
	copy(istream_iterator<string>(readfile), {}, back_inserter(nameList));
	//cout << "Vector Size is now " << nameList.size();
	
	int count;
	string lineData;
	string tmp;
	string txt;
	
	readfile >> count;
	for (int i = 0; i < count; i++)
	{
		readfile >> tmp;
		nameList.push_back(tmp);
	}

	cout << "Name List:\n";
	sort(nameList.begin(), nameList.end());
	
	for (int i=0; i < nameList.size(); i++)
	{
		cout << nameList[i];
		cout << "\n";
	}
	
	readfile.close();
	return 0;
	
}
Last edited on
I'm not very experienced in file I/O in either C or C++, but if the names are in the same format in the entire document, i.e. last name, \nfirst name\nlast name, and so on, couldn't you just separate each string by every second newline character, put the two in a single string, then cout the entire thing and repeat until the EOF?

Once again I don't know much about file I/O in C or C++, but that's how I would work around it.
Here's a nice tutorial to help:
http://www.cplusplus.com/doc/tutorial/files/

Also I think maybe using the regex string match might help too:
http://www.cplusplus.com/reference/string/string/find/
http://www.cplusplus.com/reference/cstring/strstr/

As for the regex characters available to you:
http://www.regular-expressions.info/refcharacters.html

I hope this helps,

~ Hirokachi
Still Nothing, I figured out how to display the text correctly using a getline()
but now transferring that data into a vector, I get nothing on the screen.
Nevermind

I figured it out using this as a reference

http://www.cplusplus.com/forum/beginner/93909/


I was able to display it correctly, and it sorted correctly also!

Feeling good, as I have a better understanding how to input files, display them, and dump them into vectors, and then display them.

for anyone that is wondering, or if your like myself and might find this one day here is my code!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <fstream>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main()
{
	// Empty Vector Holding All The Names from The File
	vector<string> nameList;
	
	// Read Names from File UnsortedNames.txt
	ifstream openFile("UnsortedNames.txt");

	//Pushes Each Line of Data In Text
	//To Word, Which Is Pushed to
	//The Vector nameList	
	string word;
	while (getline(openFile,word))
		nameList.push_back(word);
		
	//Sorting Using STL Liberary
	sort(nameList.begin(), nameList.end());
	
	//Output To Text File
	ofstream	outputfile;
	outputfile.open("orderedNamesText.txt");
	
	//Loop To Display Names
	//Also Outputs to Text File
	cout << "Name List:";
	outputfile << "Name List:";
	for (int i = 0; i < nameList.size(); i++)
	{
		cout << nameList[i] << '\n';
		outputfile << nameList[i] << '\n';
	}
	
}
	
Topic archived. No new replies allowed.