logo CodeStepByStep logo

chatBuddies

Write a function named chatBuddies that accepts a string parameter for a filename, where the file represents logs of chat messages. Your function must examine this file and print the pair(s) of people who converse with each other most frequently.

The input file contains a collection of chat messages, one per line, in the format of the example shown below. Each chat message begins with the sender's name, followed by a colon and a space, followed by the sender's message.

Chat messages are organized into conversations, which are groups of chat messages separated by a line of 3 dashes (---). For example, the file below contains 9 separate conversations. You may assume that a conversation involves at most two people. Note that the file ends with a --- after the end of the last conversation.

Your function should process the input file and print out the names of the pair(s) of people who participated in the largest number of conversations together. If multiple pairs of people tie for the largest number of conversations, print all such pairs. It does not matter how many individual messages the people exchange, only how many conversations they both participate in together.

Ernie: hey bert!
Ernie: did you eat yet?
Bert: no, let's grab lunch!
Ernie: yay :)
---
Ernie: good morning!
Big Bird: Hello, there!
Ernie: have a great day
---
Ernie: bert, yt?
Ernie: guess not!
---
Big Bird: I'm hungry.
Ernie: He he he!
Big Bird: Do not mock my pain
---
Bert: you see that cute dog?
Ernie: yeah I do! So cute
Ernie: I want a puppy now
---
C Monster: Who wants cookies?
Bert: Me!
C Monster: OK let's get some!
---
C Monster: Anybody else?
Ernie: Me!
C Monster: OK no problem!
---
Bert: Any cookies left?
Ernie: Nope. he he he!
Bert: :-(
---
Big Bird: hey ernie!
Ernie: studying 106B, ttyl
---

For example, if the file contains the text above, the call of chatBuddies("chatlogs.txt"); should print the following output:

Bert and Ernie
Big Bird and Ernie

The preceding is the correct output to print because both of those pairs participate in 3 conversations together. The pair of Bert and Ernie are in the first, fifth, and eighth conversations together; and the pair of Big Bird and Ernie are in the second, fourth, and ninth conversations together. The relative order in which you print the pairs, and the order of the two individual names within each pair, must be in alphabetical (ASCII) order. So printing "Ernie and Big Bird" or "Ernie and Bert" would not be acceptable.

If the file is missing or unreadable, your function should throw a string exception. If the file does exist, you may assume that it contains valid data; that is, it will contain at least one chat log, that at least one chat log in the file will be between two people, and that the file will otherwise be in the format described above. Names are case sensitive, e.g. "bert" is a different name than "BERT" or "Bert."

Constraints:

  • You should choose an efficient solution. Choose data structures intelligently and use them properly.
  • You may open and read the file only once. Do not re-open it or rewind the stream.
  • You may use as many collections as you like, but you should choose them intelligently and use them properly.
Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.