I try to use the getline function, it works in simple program and accepts the input from user but when i try to use inside the do while loop, it just escape the line and shows that there is null input. cin works fine,
case 1:
cl.append(Cb("abc",false));
cout << "text";
cout << "text1" << cl <<"&\n";

getline(cin,srch);
cl.append(Cb(srch,false));
cl.append(Cb("bbb",false));
cout << cl <<"&\n";

break;
here the cl is the instance of the object abc and Cb is the constructor defined in namespace, it accepts the first and third append but it does not take the srch variable which is of string type and display null through cl which is the operator overloaded one.

Recommended Answers

All 5 Replies

The usual problem is trying to mix say
cin >> somevar;
with getline().

They don't play well together, since a simple cin will leave a newline on the input stream, and this is exactly what getline stops at (thus giving the illusion of it being skipped).

Exactly how to get round this has been discussed endlessly on the forum and in the snippets section, perhaps look there.

Personally, I go with reading ALL input with getline, then convert from a string in memory. Trying to mix input and conversion all in one step almost always results in a mess in all but the most trivial of programs.

commented: Good advice +11

i do not have the mixing of getline and cin in the same file , i just use the cout with getline function ,and the append is the userdefined function used to insert the items in the linked list defined in a class

So post a whole program which demonstrates the fault, so we can try it ourselves.

We can't possibly guess what went wrong with a few random lines and "it doesn't work".

>> The usual problem is trying to mix say ... They don't play well together, since a simple cin will leave a newline on the input stream
the solution for this is simple (assuming that you are not interested in leading whitespaces in a line):

char ch ; string str ;
  cin.get(ch) ;
  getline( cin >> ws, str ) ;
  // user enters: X <newline> Hello World <newline>

>>> i do not have the mixing...
in which case you may be using an old compiler (and libstdc++). for example see
http://support.microsoft.com/kb/240015 in vc++6 (vc++7, vc++8 do not have these problems)
http://gcc.gnu.org/bugzilla/buglist.cgi?query_format=specific&order=relevance+desc&bug_status=__closed__&product=gcc&content=getline
in g++ mainly 3.2.x or earlier (these have been fixed in later versions).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.