User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,506 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,649 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1248 | Replies: 5
Reply
Join Date: Sep 2007
Posts: 14
Reputation: hectic is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
hectic hectic is offline Offline
Newbie Poster

Help getline function error

  #1  
Sep 29th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: getline function error

  #2  
Sep 29th, 2007
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.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: getline function error

  #3  
Sep 29th, 2007
Is that .append used correctly?
http://www.fredosaurus.com/notes-cpp...er-string.html

or is that your own class definition?
Last edited by iamthwee : Sep 29th, 2007 at 4:55 pm.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Sep 2007
Posts: 14
Reputation: hectic is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
hectic hectic is offline Offline
Newbie Poster

Re: getline function error

  #4  
Sep 29th, 2007
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
Reply With Quote  
Join Date: Dec 2005
Posts: 3,834
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 23
Solved Threads: 436
Colleague
Salem's Avatar
Salem Salem is offline Offline
banned

Re: getline function error

  #5  
Sep 29th, 2007
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".
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,085
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Rep Power: 9
Solved Threads: 163
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: getline function error

  #6  
Sep 30th, 2007
>> 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....ontent=getline
in g++ mainly 3.2.x or earlier (these have been fixed in later versions).
Last edited by vijayan121 : Sep 30th, 2007 at 9:51 am.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 3:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC