How to empty a string?

This is a discussion on How to empty a string? within the C++ Programming forums, part of the General Programming Boards category; How do I empty a string to obtain an empty line whe I "cout" it?...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    Question How to empty a string?

    How do I empty a string to obtain an empty line whe I "cout" it?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do you mean you want to write a blank line to the screen? Try:
    cout <<endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    hehe - sorry, but that's not the problem.

    The string is returned by a function an only in certain cases it must be empty...
    so I need to return an EMPTY string:
    Code:
    cout << string << endl;
    needs to give me the spare line.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well if it's a std::string, I suppose:
    std::string * s = new string(" ");

    For a char array, do:

    strcpy(s, " ");
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    28
    You can use

    return "";
    in case where an empty std::string is to be returned.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    @ Sebastiani: that's it - thank you

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Functions clear() and erase() in the string class as well as over assignment operators do not assume that you want to clear everything from a string when you use one of the functions or the operator. From an STL string class perspective, a string object can hold more than just characters.

    One solution is to set the string object array to NULL and then call clear().

    Code:
    string sText;
    
    // Get a line of data from cin buffer.
    // getline() terminates on ENTER key.
    std::getline(cin, sText);
    
    // Clear the entire object.
    sText[sText.size()] = NULL;
    sText.clear();
    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21