Concordance Program

Hi! So what I need to do is write a program that reads in a file (Modest Proposal) and sorts all the words in alphabetical order after converting all of them to lowercase and removing most of the punctutation and sends them to a file (concordanceFile) with the line numbers that they all appear on next to them. I tried soo many things for the lines part, and I commented out one way I tried that didn't work. And I am stuckk. Any help would be appreciated!

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
#include <fstream>
#include <vector>
#include <locale>
#include <iterator>
#include <sstream>

using namespace std;

int main()
{
		ofstream concordanceFile;
		concordanceFile.open("concordanceFile");

		//Modest Proposal
			vector<string> wordsBook1;
			string line;
			vector<int> lines;
			string word;
			ifstream MProposal;
			int count = 1;
			MProposal.open("ModestProposal_concordance.txt");

			if (MProposal.is_open()) {
				cout << "File has been opened! " << endl;
			}
			else {
				cout << "File has not been opened! " << endl;
			}
			while (MProposal >> word) {
				//To convert all words to lowercase
				transform(word.begin(), word.end(), word.begin(), ::tolower);
				//To remove punctuation
				for (int i = 0, len = word.size(); i < len; i++)
				{
					if (ispunct(word[i]) && word[i] != '\'' && word[i] != '-' && word[i] != '.')
					{
						word.erase(i--, 1);
						len = word.size();
					}
				}
				wordsBook1.push_back(word);
			}
			/*while (getline(MProposal, line)) {
			lines.push_back(count);
			count++;
			}*/
			sort(wordsBook1.begin(), wordsBook1.end());
			for (int i = 0; i < wordsBook1.size(); i++) {
				concordanceFile << wordsBook1[i] << endl;
				//concordanceFile << "Line: " << lines[i] << endl;
			}
	
	system("pause");
	return 0;
}
The sort function requires a third parameter. See:

http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
Are there any constraints of what you can use?

I would store it in a map<string, vector<int>> since you may have many line numbers for one word.
Topic archived. No new replies allowed.