Thread: Subtracting from strings?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Subtracting from strings?

    How would I go about doing this? Here's what I have that works:

    Code:
    string shipheight = "\n\n\n\n\n\n\n\n\n\n\n\n";
    shipheight += "\n";
    This would come out with one more line. This doesn't work though:

    Code:
    shipheight -= "\n";
    I want it to take away a line. How could I do that?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, assuming that you know for sure that the newline would be at the end:
    Code:
    if (!shipheight.empty())
    {
    	shipheight.erase(shipheight.end() - 1);
    }
    If you have to search, then:
    Code:
    if (!shipheight.empty())
    {
    	std::string::size_type pos = shipheight.rfind('\n', shipheight.length() - 1);
    	if (pos != std::string::npos)
    	{
    		shipheight.erase(shipheight.begin() + pos);
    	}
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    or use substr().
    Code:
    if (!shipheight.empty())
    {
    	std::string::size_type pos = shipheight.rfind('\n', shipheight.length() - 1);
    	if (pos != std::string::npos)
    	{
    		shipheight = shipheight.substr(0,pos);
    	}
    }

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Quote Originally Posted by laserlight
    Well, assuming that you know for sure that the newline would be at the end:
    Code:
    if (!shipheight.empty())
    {
    	shipheight.erase(shipheight.end() - 1);
    }
    Thanks, that worked fine.

    One more question: How can I make my program take input and make it do something while something else is happening?

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I think what you want is Multithreading
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Please start a new thread for a completely new topic.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't nessesarily need to use multithreading. If your compiler supports it, you might be able to use functions like kbhit() to determine whether there's a key in the input queue or not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    3
    Quote Originally Posted by dwks
    You don't nessesarily need to use multithreading. If your compiler supports it, you might be able to use functions like kbhit() to determine whether there's a key in the input queue or not.
    Thanks, took me a while to figure it out, but that worked great.

    I'll post a new thread for different problems from now on...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 11:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM