sets

closed account (DG6DjE8b)
I am totally lost on how to do this. Could someone give me some input?

For the program, the input file will be limited to integers (int). The purpose of the program is to utilize the set<int> class to store the integers listed in the input fi le (prog1 input.txt.) Each row must be stored as a set<int>.

Example input for prog1
SetName Contents
A I 10 I 13 I 47 I 9 I 20
B I 9 I 13 I 10 I 40
C I 10 I 9 I 70
For the output, the header row must be formatted SetNamentContents. Each value row mustbe formatted <SetN ame>nt<value1>, <value2>, ...

Example output for prog1
SetName Contents
A 9, 10, 13, 20, 47
B 9, 10, 13, 40
1
2
3
4
5
6
7
8
9
For each line:
    read the line with getline
    use an istringstream to parse the line you read to pick out the set name and values
    for each value
        add the value to a set of int

    write the set name
    for each int in the set
        write the value
Last edited on
closed account (DG6DjE8b)
So i got this far. I got the file to read and all.. but how do i delete the "I".

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;

int main() {
fstream ifile("prog1_input.txt");
if (!ifile.good()) {
cerr << "Unable to open prog1_input.txt" << endl;
return 255;
}

string line;
getline(ifile, line);

int lcount=0;
while (getline(ifile, line))
{

istringstream iss(line);

int ccount=0;
char p;
string col;
while (iss >> col)
{

cout << ccount << ":" << col;

ccount++;
}
cout << endl;
lcount++;
}
ifile.close();

return 0;
}

I've formatted your code so I can see it.
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
#include<iostream>
#include<fstream>
#include<sstream>

using namespace std;

int main()
{
	fstream ifile("prog1_input.txt");
	if (!ifile.good()) {
		cerr << "Unable to open prog1_input.txt" << endl;
		return 255;
	}

	string line;
	getline(ifile, line);

	int lcount=0;
	while (getline(ifile, line))
	{
		istringstream iss(line);

		int ccount=0;
		char p;
		string col;
		while (iss >> col)
		{
			cout << ccount << ":" << col;
			ccount++;
		}
		cout << endl;
		lcount++;
	}
	ifile.close();

	return 0;
}


This is your code with my psuedo-code implemented.
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<iostream>
#include<fstream>
#include<sstream>
#include<set>

using namespace std;

int main()
{
	fstream ifile("prog1_input.txt");
	if (!ifile.good()) {
		cerr << "Unable to open prog1_input.txt" << endl;
		return 255;
	}

	string title;
	getline(ifile, title);
	std::cout << title << std::endl;

	// For each line:
//	int lcount=0;

	// read the line with getline
	string line;
	while (getline(ifile, line))
	{
		// use an istringstream to parse the line you read to pick out the set name and values
		istringstream iss(line);

		std::string name;
		iss >> name;

//		int ccount=0;
//		string col;
		set<int> numbers;
		std::string seperator;
		int value;
		while (iss >> seperator >> value)
		{
//			cout << ccount << ":" << col;
//			ccount++;
			numbers.insert(value);
		}
//		cout << endl;
//		lcount++;

		// write the set name
		std::cout << name;

		// for each int in the set
		for (set<int>::const_iterator iter = numbers.begin(); iter != numbers.end(); ++iter)
		{
			// write the value
			std::cout << " " << *iter;
		}
		std::cout << std::endl;
	}
//	ifile.close();

	return 0;
}


We don't need counts, so they've been removed.

I've added a set, added numbers into it and displayed them.

Please let us know if you don't quite understand what's changed.
Last edited on
Topic archived. No new replies allowed.