Thread: Are there other string classes?

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    51

    Are there other string classes?

    Hi, I'm getting pretty sick of char * and all other things C++ fail with when I need to type in and store some text. All I want is no extra characters. No flushing buffers, cin.ignore() and things. No keeping track of what works when, ever! However, I do need to be able to convert back to c-strings because i use the strings with winsock-functions like send and recv.

    So are there any classes that succeed with the complicated task of reading user input and storing it in an unchanged form?

    PS the built in sring-class sucks too
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, then the question becomes, what's wrong with the built-in string class? It sounds like all your problems are input-related, which means you should probably be using more better input commands, like getline, instead of trying to use <<.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    std::string line;
    std::getline(std::cin, line);
    No extra characters. No flushing buffers.
    You will not be able to escape keeping track of if it worked or not, however.
    Also supports converting it back to a C-style string.

    If you need a specific type of data, then I have posted an input class on the board that pretty much handles it all for you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    51
    Quote Originally Posted by tabstop View Post
    So, then the question becomes, what's wrong with the built-in string class? It sounds like all your problems are input-related, which means you should probably be using more better input commands, like getline, instead of trying to use <<.
    I've used all there is. Sometimes one method works, sometimes another does. Sometimes I have to add cin.ignore() here and there, sometimes i don't need too. Thing is, I don't want to be forced to keep track of this anymore. I want one function read(mystring); that reads from my keyboard to mystring until I hit enter. And it would be nice if it worked flawlessly every time.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    On the other hand, getline can only go bad if it reads end-of-file, I believe (or some complete input disaster happens), not just because the input doesn't match some format or other. I'm often a bad person and don't check whether getline fails.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    51
    Elysia, ok that works. But not always when used mixed with cin.get(). Maybe I've blamed the string-class because cin.get screws it up.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is. Best not to mix them, or you will run into the same problem again.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It seems to me that std::geline and cin.get() get along just fine. It's operator>> that's guilty of leaving stuff into the buffer (I guess that has some purpose though, so you can for example see what the delimiter was).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, get also leaves the delimiting character in the buffer too (hence all the calls to .ignore).

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Ah yes, my bad.

    In any case you don't need a new class. Perhaps wrapping the input functions up in functions that automatically clean the buffer is enough?
    Last edited by anon; 12-21-2008 at 05:27 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tabstop
    So, then the question becomes, what's wrong with the built-in string class?
    It is a monolithic class

    (Though technically it is not "built-in" since it is part of the standard library, not the core language, but anyway finnepower did not have Herb Sutter's view in mind with that comment.)

    Quote Originally Posted by anon
    Perhaps wrapping the input functions up in functions that automatically clean the buffer is enough?
    Yes, that sounds like a viable approach to me. Simply not mixing formatted input functions with get() and getline() is not always the best solution, in my opinion, since on occasion you may want to use both.
    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

  12. #12
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You are wise, laserlight.

    It often seems like guesswork to me as to when I'll need an ignore(). Does this always blast past the user?
    Code:
    std::cin >> type; //user makes a '\n' to indicate entry
    cin.get(); //maybe the buffer is empty and we'll wait... maybe there's an extra \n and we won't?
    For me, cin consistently leaves the delim in the stream. I can't seem to find the sentence in Stroustrup that clarifies this.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The only legitimate way I can think of, using >>, to possibly not have a \n character left over is to set noskipws and read in a single character. But \n is not considered a valid part of any of the primitive datatypes (except char, and then only when noskipws is set) so it will be left in the input stream.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CodeMonkey
    It often seems like guesswork to me as to when I'll need an ignore(). Does this always blast past the user?
    What happens if the user input has trailing whitespace? One way is to simply ignore as many characters as possible that may be left in the buffer until and including the next newline character:
    Code:
    std::cin >> type;
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    Another way is to specifically extract and discard whitespace:
    Code:
    std::cin >> type;
    std::cin >> std::ws;
    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

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    std::cin >> type;
    My input class performs this beautifully without the need to worry for cleaning the input.
    So if that sort of thing is what you need, I may have the solution wrapped inside a class, which I posted some time ago.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. += operator
    By BKurosawa in forum C++ Programming
    Replies: 8
    Last Post: 08-05-2007, 03:58 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM