Facing trouble with maps.

I've been assigned a problem, and I am really stuck right now. Here's the statement.
The students of a university are able to enroll in
various activities (groups). Each group name is displayed in capital letters,
while the student's user name in lowercase. Write
a program that calculates the total number of students in each group.
Those students enrolled in more than one groups or those
registered in the same group multiple times will not be calculated in
total. The data input is terminated by 0.

Example:
BOXCLUB
jason
mike78
pikkias
READBOOK
eagle
twilly
mike78
SPORTSCIENCE
optimum32
optimum32
0

Output:
BOXCLUB 2
READBOOK 2
SPORTSCIENCE 1

I've been trying for almost 2 hours and I came up just with the following epic failure.
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
  #include<iostream>
#include<utility>
#include<map>

using namespace std;

int main(){
    string input;
    map <string, int> groups;
    int groupn = 0;
 while(!cin.eof()){
    cin>>input;
    next:
    if(input[0]>='A' && input[input.size()-1]<='Z'){
        groups [input] = groupn;
        while(!cin.eof()){
            cin>>input;
            groupn ++;
            int k;
            k = input.size()-1;
        if (input[0]>='A'&&input[k]<='Z'){
            groups[input] = groupn;
            groupn = 0;
            goto next;
        }
        else if(input=="0"){
            break;
        }
        }
    }
    if (input=="0"){
        break;
    }
 }
 typedef map<string, int>::const_iterator MapIterator;
for(MapIterator iter=groups.begin(); iter!=groups.end(); iter++ ){
    cout<<iter->first<<" "<<iter->second<<endl;
}

}


Help in any way would be very much appreciated. Thanks :P
Last edited on
do you have to use maps?
I would create one class to store a data of a group(groupname and a collection of students).
One class to store all the groups and doing the calculations and display.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Group
{
public:
  Group(string name);
  void AddMember(string name);
  int GetMemberCount();
  string GetName();
private:
  string m_Name;
  set<string> m_Members;
};

class University
{
public:
  void AddStudent(string student_name, string group_name);
  int GetGroupCount();
  Group& GetGroup(string name);
  int GetStudentCount();
private:
  vector<Group> groups;
  set<string>students;
};


Also your data input could be much simpler - like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  string group_name, student_name;

  for(;;)
  {
    cout << "Enter group name: ";
    cin >> group_name;
    if (group_name == "0")
      break;

    cout << "\nEnter student name: ";
    cin >> student_name;
    if (student_name == "0")
      break;

    // add the student
  }


This is not the complete code - just a suggestion.
I have to work now so I can't give you the full code, maybe you can try for yourself.
@Thomas1965 Thanks for the help. Also, I never want to be given already written code that I will just copy and paste. I need to understand what I'm doing otherwise I'll never increase my knowledge.
One way to do 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
38
39
40
41
42
43
44
45
46
47
48
#include <cctype>
#include<iostream>
#include <map>
#include <set>
#include <string>
#include<utility>

int main() 
{
    // students per group
    std::map<std::string, std::set<std::string>> group_members;

    // group membership count per student
    std::map<std::string, unsigned> group_counts;

    std::string current_group;
    std::string token;
    while (std::cin >> token && token[0] != '0')
    {
        if (isupper(token[0]))                  // new group
        {
            current_group = token;
            group_members[current_group];       // empty list for empty group
        }
        else
        {
            auto& student_name = token;
            auto& members = group_members[current_group];

            if (!members.count(student_name))   // not a member of this group?
            {
                members.insert(student_name);
                ++group_counts[student_name]; 
            }
        }
    }

    // Eliminate students who appear in more than one group.
    for (auto& student : group_counts)
    {
        if (student.second > 1)
            for (auto& group : group_members)
                group.second.erase(student.first);
    }

    for (auto & group : group_members)
        std::cout << group.first << ' ' << group.second.size() << '\n';
}
It's actually simpler that I initially thought. Thanks for the help guys!
Topic archived. No new replies allowed.