behinddwalls
behinddwalls

Reputation: 618

input in c++ - cin.getline

#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 ?

Upvotes: 2

Views: 8441

Answers (3)

johnathan
johnathan

Reputation: 2355

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.

Upvotes: 1

Ritesh Kumar Gupta
Ritesh Kumar Gupta

Reputation: 5191

(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.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490048

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.

Upvotes: 1

Related Questions