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