943,857 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 515
  • C++ RSS
Aug 2nd, 2009
0

Simple question about getline and "deliminating character"

Expand Post »
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!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
iamsmooth is offline Offline
49 posts
since Jul 2009
Aug 2nd, 2009
0

Re: Simple question about getline and "deliminating character"

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!).
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 3rd, 2009
0

Re: Simple question about getline and "deliminating character"

It would, as VernonDozier suggested, be easy enough to code your own version...

Here's a simple stab at it:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 2d Library for noobs
Next Thread in C++ Forum Timeline: gotoxy function in turbo c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC