2
#include<iostream>
#include<cstdio>
using namespace std;

int main()
{
    int T;
    char J[100], S[100];
    int count=0;
    cin >> T;

    while(T--)
    {
        cin.getline(J,100);

        cin.getline(S,100);
        puts(J);
        puts(S);

        for(int i=0; J[i]!='\0'; i++)
        {
            for(int j=0; S[j]!='\0'; j++)
            {
                count++;
                for(int k=j; S[k]!='\0'; k++)
                    S[k]=S[k+1];
                break;
            }
         }
         cout << count;
    }
    return 0;
}

I am taking input string in J,S but during execution of program it skips the second input from the console cin.getline I takes the test cases then takes the Strings J and S it takes J successfully but fails to get S string ?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
behinddwalls
  • 644
  • 14
  • 39
  • `cin >> x` leaves a newline in the input buffer. Clear it out with `cin.ignore` if you're `getline`ing something afterwards. – chris May 09 '12 at 03:33
  • and this is a 'c++' question, cin and getline aren't part of 'c' – Martin Beckett May 09 '12 at 03:35
  • @MartinBeckett: I re-tagged the question as recommended. – stanigator May 09 '12 at 03:39
  • possible duplicate of [getline not asking for input?](http://stackoverflow.com/questions/6642865/getline-not-asking-for-input) – Martin York May 09 '12 at 03:46
  • 1
    The first thing you should do is forget that `cin.getline` exists at all, and instead use `std::getline(std::cin, your_string);` instead. It may or may not fix the problem you're seeing, but it'll definitely fix a lot. – Jerry Coffin May 09 '12 at 03:51
  • @JerryCoffin the namespace scope getline requires the stream object to be in the 1st parameter. – johnathan May 09 '12 at 03:52
  • @johnathon: oops. Quite right. – Jerry Coffin May 09 '12 at 03:53
  • @JerryCoffin know whats even funnier, the OP is checking for null character but he's not initializing his array, compiler dependent on what it gets initialized with. – johnathan May 09 '12 at 03:55

3 Answers3

1

you need to be using a string , std::string, and calling getline as in

std::string s,j;
std::getline(std::cin,j);
std::getline(std::cin,s);

and then if you want to iterate over the contents of the strings by individual characters

for(auto i = std::begin(s); i != std::end(s); ++i)
{
     std::cout << *i << std::endl;
}

use the iterators, and deference them to get the actual character values. Stay way from c strings as much as possible.

johnathan
  • 2,315
  • 13
  • 20
1

This code:

int T;
// ...
cin>>T;

...reads an int from standard input. To get it to read that int, you need to press the enter key, but this code does not remove that enter from the input buffer. Then...

cin.getline(J,100);

This tries to read a string from the input buffer, up to the first new-line character. It removes that new-line from the input buffer, but does not include it as part of the string. As such, if you don't look really closely, it appears to do essentially nothing -- i.e., you end up with an empty string.

You generally want to stick to either field-oriented input (like your cin >> T; or else line-oriented input (getline). Mixing the two, however, can be a bit tricky, especially if the getline comes after the field-oriented input. Don't get me wrong: it can work -- and work perfectly well at that, but you need to know what you're doing, and even then it can still surprise you now and again.

As noted in my comment (and @johnathon's answer) you also generally want to use std::getline to read an std::string, instead of std::cin.getline with an array of char. The latter is clumsy to deal with, even at best.

My own preference is (as a rule) to use line-oriented input throughout if you're going to use it anywhere.

std::string temp;

std::getline(std::cin, temp);

int T = lexical_cast<int>(temp);

while (T--) {
   std::string j;

   std::getline(std::cin, j);

   // ...

As an aside, I'd also avoid using T as a name of an ordinary variable in C++. It's quite commonly used as the name of a template parameter; using it for "normal" variables is more likely to lead to confusion, especially for more advanced programmers who use templates more often.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

(1)Use getchar() between cin>>T and while (T--) as

int T;
char J[100] , S[100];
int count=0;
cin>>T;
getchar();
while(T--){
cin.getline(J,100);
cin.getline(S,100);

(2). You can also resolve your problem in following way::

char  T[10];
char J[100] , S[100];
int count=0;
getline(T,10);

while((atoi(T))--){

cin.getline(J,100);

cin.getline(S,100);

use Any 1 of them ,it will fix your problem.

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71