Need a lil advice from more experienced ones

Hi, Im trying to write a program that reads from text file how many times each "patient" occurs in a text file.
Example:

P1 D1 M1
P1 D2 M1
P2 D3 M2 M3
P2 D4 M4
P2 D3

and then outputs into another textfile that
Patient 1 has 2 diagnosis
patient 2 has 3 diagnosis
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>

using namespace std;

// I etapp: Multimorbiidsus
vector<string> read_file() { // Function reads every row and returns these
	vector<string> contents;
	ifstream input("diagnoosid.txt");
	if (!input.is_open()) {
		cout << "Error! Opening file was unsuccesful." << endl;
	}
	string row;
	while (getline(input, row)) {
		contents.push_back(row);
	}
	input.close();
	return contents;
}

void write_file(const vector<string>& contents, const string& filename) {
	ofstream output(filename);
	if (!output.is_open()) {
		cout << "Error! Opening file was unsuccesful" << endl;
		return;
	}
	for (const string& row: contents) {
		output << row << endl;
	}
	output.close();
}

multimap<string, string, ltstr> multimorbidity(const vector<string>& contents) {
	string word = "";
	string key = "";
	bool continuation = true;
	int space_counter = 0;
	for (int i = 0; i < rida.lenght(); i++) {
		char c = nextline[i];
		if (isspace(c)) {
			if (word.lenght > 0) {
				if (space_counter == 0) {
					key = word;
				}
				else if (space_counter == 1) {
					for (multimap<const char*, int, ltstr>::iterator it = m.begin(); it !=m.end(); ++it){
						if (it->first.compare(key) == 0 && it -> second.compare(word) == 0) {
							continuation = false;
							break;
						}
					}
					if (continuation == false) {
						break;
					}
					else {
						map.insert(pair <string, string> (key, word));
					}
				}
			}
			space_counter ++;
		}
		if (space_counter == 0) {
			key += c;
		}
		else {
			word += c;
		}
	}
}

int main(){
	string newfile = "multimorbiidsus.txt";
	vector<string> patients = read_file();
	//map<string, unsigned int> multimorbiidsus = multimorbidity(patients);
	cout << patients.size() << endl;
	//write_file(multimorbidity, newfile);
	return 0;
}


i have no idea what should I implement
Does the fact that D3 is repeated twice mean anything?
If you just need to count the number of 'diagnoses', you don't need a multimap; a simple map<string, int> would do.

You appear to have some functions written, but you don't call them from main. What is the problem you're having?
Your multimorbidity function claims to return a multimap<string, string, ltstr>, but you never return anything from this function.
Note that a multimap is not a map. Again, I don't think you need to use a multimap here.

- rida.lenght(); and this is just simple typos you need to watch out for.
- what is an 'ltstr'? (line 38)
- where is 'rida' even defined?
Last edited on
The fact "d3" doesn't matter at all
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>

using namespace std;

// I etapp: Multimorbiidsus
vector<string> read_file() { // Function reads every row and returns these
vector<string> contents;
ifstream input("diagnoosid.txt");
if (!input.is_open()) {
cout << "Error! Opening file was unsuccesful." << endl;
}
string row;
while (getline(input, row)) {
contents.push_back(row);
}
input.close();
return contents;
}

void write_file(const vector<string>& contents, const string& filename) {
ofstream output(filename);
if (!output.is_open()) {
cout << "Error! Opening file was unsuccesful" << endl;
return;
}
for (const string& row: contents) {
output << row << endl;
}
output.close();
}

set<string>

int main(){
string newfile = "multimorbiidsus.txt";
vector<string> patients = read_file();

write_file(patients, newfile);
return 0;
}

This is the earlier version of the code, in the original I posted, I've started to try to implement "map"
The issue is I'm having a difficult time getting anything to work, I'm not exactly sure or how I should get on with it :D
Last edited on
@hitasa - Please format you code before posting and use code tags so that the code is readable:

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

vector<string> read_file() { // Function reads every row and returns these
	vector<string> contents;
	ifstream input("diagnoosid.txt");

	if (!input.is_open()) {
		cout << "Error! Opening file was unsuccesful." << endl;
	}

	string row;

	while (getline(input, row)) {
		contents.push_back(row);
	}

	input.close();
	return contents;
}

void write_file(const vector<string>& contents, const string& filename) {
	ofstream output(filename);

	if (!output.is_open()) {
		cout << "Error! Opening file was unsuccesful" << endl;
		return;
	}

	for (const string& row : contents) {
		output << row << endl;
	}

	output.close();
}

int main() {
	string newfile = "multimorbiidsus.txt";
	vector<string> patients = read_file();

	write_file(patients, newfile);
	return 0;
}

Is this something like what you want:

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
#include <string>
#include <map>
#include <fstream>
#include <iostream>
#include <sstream>

int main()
{
	std::ifstream ifs("multimorbiidsus.txt");

	if (!ifs)
		return (std::cout << "Cannot open input file\n"), 1;

	std::map<std::string, size_t> pats;

	for (std::string line, pat; std::getline(ifs, line); ) {
		std::istringstream iss(line);

		iss >> pat;
		++pats[pat];
	}

	for (const auto& [pat, diag] : pats)
		std::cout << pat << "  " << diag << '\n';
}


Given the input file from the first post, displays:


P1  2
P2  3

Topic archived. No new replies allowed.