Simple question about getline and "deliminating character"

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 35
Reputation: iamsmooth is an unknown quantity at this point 
Solved Threads: 0
iamsmooth iamsmooth is offline Offline
Light Poster

Simple question about getline and "deliminating character"

 
0
  #1
Aug 2nd, 2009
So my program is taking input from the user, so it's storing it in a string known as x. But I don't want x to take the input after a hard return, it should keep reading until it hits a certain string, let's say "END", so until the user types END, the variable will take everything until that end.

Using getline() you can add a "deliminating character" so that when a CHAR is read, it will stop. i.e. getline(cin, x, 'Q') will keep getting more input until the user types Q followed by a hard return. Is it possible to have a "deliminating string" instead of the character? I already tried it with getline, and it doesn't seem to help.

I need my input to end when the user types "END".
I tried getline(cin, x, "END"), but to no avail.

Appreciate the help once again!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Simple question about getline and "deliminating character"

 
0
  #2
Aug 2nd, 2009
I don't think you are going to find anything pre-written like that with getline or anything else. You want something that both reads in input AND searches for a string at the same time. I don't know of anything like that that exists, and I don't see anything like that with getline. I think you have to read in data, THEN check for the delimiter using "find" from string or something like that. In other words, two separate tasks. One, read in data from the stream. Two, check the stream to see whether you've gone far enough. Possibly step three: Check to see if you've gone "too far" and if so, adjust the string that holds what you've read in and possibly "put back" characters into the stream.

You should probably read a character at a time, then check whether "END" has appeared yet, then continue or stop based on that. There may be something pre-written out there (it comes up a lot, so there probably is), but if it's in the standard libraries, I don't know what it is (which doesn't mean it doesn't exist!).
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Simple question about getline and "deliminating character"

 
0
  #3
Aug 3rd, 2009
It would, as VernonDozier suggested, be easy enough to code your own version...

Here's a simple stab at it:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::istream& getline( std::istream& ins, std::string& result, const std::string& terminator )
  5. {
  6. char c;
  7. result.clear();
  8. while (ins.get( c ))
  9. {
  10. result.push_back( c );
  11. if (result.rfind( terminator ) == (result.length() - terminator.length()))
  12. {
  13. result.resize( result.length() - terminator.length() );
  14. break;
  15. }
  16. }
  17. if (result.length()) ins.clear();
  18. return ins;
  19. }
Hope this helps.
Last edited by Duoas; Aug 3rd, 2009 at 3:38 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC