0

This is my code here i am trying to find the biggest length of same length characters at a time like "a a a bb bb bc sa sa a a" so answer is 5 for two characters at a time for 5 times adjacently .

this is my code , my question is that when i am trying to take the input , for my first input it is not going to getline but printf in last lines and then it takes a line and prints output

like if i give 5 it writes 1 then it takes getline , but i want it to take getline first rather than printf, in this way for my 5 input it prints 1 and 4 desired outputs .i want 5 desired can you tell me why..

#include<iostream>
#include<cstdio>
#include<vector>

using namespace std;

int main()
{
    int a,j;
    scanf("%d",&a);
    for(j=0;j<a;j++)
    {
        vector<int> v;
        string s;
        getline(cin,s);

        int i,cnt =0;
        for(i=0;i<s.length();i++)
        {

            if(s[i] != ' ')
            {
                cnt++;
            }
            else
            { 
                v.push_back(cnt);
                cnt =0;
            }
        }
        v.push_back(cnt);

        int k=0;
        int ps =0;
        int sp=0;

        while(k<v.size()-1)
        {
            if (v[k+1] - v[k] == 0)
            {
                sp++;
                k++;
            }
            else
            if (sp >= ps)
            {
                ps = sp;
                 k++;
                sp=0;
            }
            else
            {
                k++;
                sp=0;
            }
        }
        if (sp<ps)
        printf("%d",ps+1);
        else
        printf("%d",sp+1);
    }

    return 0;
}    
Mat
  • 202,337
  • 40
  • 393
  • 406
Sudhanshu Gupta
  • 2,255
  • 3
  • 36
  • 74

2 Answers2

1

You shouldn't be mixing scanf with getline, try to only use one or the other (and getline is a better option).

What is happening is that scanf stops parsing once it has finished reading an int. It does not consume the end of line character you type, or anything else you could have entered after that number. So getline picks up whatever was left on that first line (possibly just the newline char).
A "quick" but dirty fix would be to change your scanf call so that it swallows the whitespace after the int:

scanf("%d ",&a);
      // ^ notice the space there

But that's not a real fix. Use getline and a string stream (in <sstream>) to get the first number, and your code will work as you intend it to. You'll find examples of using the istringstream to extract a number in this FAQ: How to convert a number to string and vice versa in C++

You might also be interested in this other question: Split a string in C++?, the answers demonstrate ways to split a string that are less error-prone than what you're doing here.

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
1

How are you entering everything exactly? I think the problem may be that your getline() is reading your last [enter] off of the stream, thus automatically putting an empty string into s. That would result in the 1 you're getting. Try this for your scanf:

scanf("%d\n",&a)

That should absorb the [enter].

benjer3
  • 657
  • 6
  • 13