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 file (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
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
#include<iostream>
#include<fstream>
#include<sstream>
#include<set>usingnamespace 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.