hello,
I have an assignment for my one class at school where I am supposed to write a program that creates lists of people who have met. Each line of input is a space-separated list of the names of people who have met. If a person is in a group with someone, they have met and should be on each other's contact list. The input is multiple lines of groups of people terminated by end-of-file
for example:
beth david mark helen
helen ben faheem eric
mavia sarah mindy
...etc
I am allowed to use any C++ data structures, and I was planning on using a map of strings and sets like map<string, set<string> >,
but I really don't know how to separate the names to store in the sets. Can anyone help me?

Recommended Answers

All 3 Replies

I'm not real sure what your question is. Are you trying to separate the names as you input them? Separating them is the easy part.

The hard part is managing the data after the input. I think you could do something with nested loops and a vector of strings.

Keeping in mind that the extraction operator interrupts the stream at any whitespace (which a space is) you should be able to use the first inner loop to get your input and store it into a vector. You would then use a second inner loop to process the information and set up your maps and sets.

The outer loop would be a main application loop used for "do again" type things.

visual aids help:

// some declarations
char addAnother = 'y';
string name;
string nextName;
vector<string> inNames;

// outer loop
while (addAnother = 'y') {

  //inner loop 1, used for input
  while (cin >> nextName) {
    inNames.push_back(nextName);
    // other required actions
  }

  // inner loop 2, used for processing and storing
  // while (...) or for (...) depending on previous code structure and preferences

  // prompt for addAnother

} // close outer loop

Someone else may have a better idea, but given my limited abilities, this is how I would do it.

That's kinda what I was looking for the only problem is that once it runs through the input from the first line, how do I get the next line to be placed into a different vector or whatever data structure I use?

That's what the outer "addAnother" loop is for. I did not show all required action because 1.) I simply don't know them all right now and 2.) I'm not technically allowed to give you cut-and-paste code.

Basic algorithm (additional steps will be necessary):
1. Enter the outer loop
2. Enter the first inner loop.
3. Accept and store the input using the first inner loop.
4. Exit the first inner loop
5. Enter the second inner loop.
6. Traverse the vector and process the strings appropriately.
7. Call std::vector::clear() to reset the vector.

The variables associated with the outer vector are all re-used, they are only "temporary" variables intended to facilitate the input. You then use the data in the temporary variables to build and populate your map(s) and sets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.