Jul 6, 2014 at 3:45pm Jul 6, 2014 at 3:45pm UTC
1 2 3 4 5 6
int t;
string a;
cin>>t;
getline(cin,a);
int len=a.length();
cout<<a<<" " <<len;
Input:
5 hello hi
Output:
<space>hellohi 0
why is the length 0?what can I do to get the correct length of the input string?
Last edited on Jul 6, 2014 at 3:47pm Jul 6, 2014 at 3:47pm UTC
Jul 6, 2014 at 4:21pm Jul 6, 2014 at 4:21pm UTC
When I compile and run the code I get the following output: hello hi 9
Jul 6, 2014 at 4:27pm Jul 6, 2014 at 4:27pm UTC
I... don't know. Perhaps its because you are using an int. Maybe using a stringstream would fix it( convert t to a string). It is defined in the header <sstream>. I suggest you read up on that
Jul 6, 2014 at 6:54pm Jul 6, 2014 at 6:54pm UTC
@George Kuriakose is right.
But, since you are using C++, use the header:
#include <cstring>
Hope this helped :)