0

Its working fine with codeblocks using stdin and stdout, but when i am using freopen to redirect stream or running code in linux with "g++" , the input/output shows erratic behaviour !!! can anyone please tell me what might be the possible issue.

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,string> p;
int main()
{
// freopen("11.txt","r+",stdin);
// freopen("out.txt","w",stdout);
 int n,t,i,j,k;
string tmp;
cin>>t;
for(i=1;i<=t;i++)
{
    cin>>n;
    fflush(stdin);
priority_queue<p,vector<p>,less<p> > s;
    for(j=0;j<n;j++)
    { getline(cin,tmp);
        map<char,int> mp;
        int c=0;
        for(k=0;k<tmp.length();k++)
        {
            if(tmp[k]!=' '){
            if(mp.find(tmp[k])==mp.end())
            {c++;
              mp[tmp[k]]++;
            }
        }
        }
        if(!s.empty()&&s.top().first<c)
                { s.pop();}
                    s.push(make_pair(c,tmp));
    }
     p ans;
 if(!s.empty())
   ans =s.top();

   string qwe = ans.second;
  cout<<"Case #"<<i<<": "<<qwe<<endl;
  fflush(stdin);
}

return 0;
 }

1 Answers1

0
cin>>n;

followed by

getline(cin,tmp);

doesn't work since the first line usually leave a newline character in the input stream.

Add a line of code to ignore rest of the line from the input stream.

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

right after

cin >> n;

Make sure to add

#include <limits>

to use std::numeric_limits.

R Sahu
  • 196,807
  • 13
  • 136
  • 247