ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1446. Sorting Hat

Yermakov Alex <ONPU> Something with getline() function . Please let me know... [2] // Problem 1446. Sorting Hat 29 Apr 2010 23:39
#include <iostream>
#include <string.h>
int main() {
    char grif[1001][202],sliz[1001][202],huff[1001][202],rave[1001][202],name[202],group[20];
    int n,i,gcur=0,scur=0,hcur=0,rcur=0;
    std:: cin >> n;
    while( n>=0 )
    {
                  std:: cin.getline(name,202);
                  std:: cin.getline(group,20);

                                if(!strcmp(group,"Slytherin"))  { strcpy(sliz[scur],name); scur++; }
                                if(!strcmp(group,"Hufflepuff")) { strcpy(huff[hcur],name); hcur++; }
                                if(!strcmp(group,"Gryffindor")) { strcpy(grif[gcur],name); gcur++; }
                                if(!strcmp(group,"Ravenclaw"))  { strcpy(rave[rcur],name); rcur++; }

                  --n;
    }

    std::cout<<"Slytherin:\n";
    for( i=0; i<scur; ++i ) std:: cout << sliz[i] << '\n';
    std::cout<<'\n';

    std::cout<<"Hufflepuff:\n";
    for( i=0; i<hcur; ++i ) std:: cout << huff[i] << '\n';
    std::cout<<'\n';

    std::cout<<"Gryffindor:\n";
    for( i=0; i<gcur; ++i ) std:: cout << grif[i] << '\n';
    std::cout<<'\n';

    std::cout<<"Ravenclaw:\n";
    for( i=0; i<rcur; ++i ) std:: cout << rave[i] << '\n';
    std::cout<<'\n';

    return 0;
    }
This is madness! Don't write such code =)

int main()
{
  int students;
  cin >> students;

  map< string, vector<string> > house_to_students;
  for (int student = 0; student < students; student++)
  {
    cin.ignore();

    string student_name;
    getline(cin, student_name);

    string house;
    cin >> house;

    house_to_students[house].push_back(student_name);
  }

  for (auto house : { "Slytherin", "Hufflepuff", "Gryffindor", "Ravenclaw" })
  {
    cout << house << ":" << endl;

    for (auto student : house_to_students[house])
      cout << student << endl;

    cout << endl;
  }
}
It happened bcs of cin function
In the input they give n and endl after n;
so, somehow getline reads this endl as a string, so you can do this
insted of "cin>>n"
use "scanf("%d\n", &n);