Thread: Are there other string classes?

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    There are several common issues with using cin and probably quite a few more that aren't common.

    For example, if you don't need to handle bad input, then you can get away with always calling cin.ignore() after calls to cin >> to ignore the trailing newline.

    If you do want to handle error conditions, then something like this usually works:
    Code:
    // prompt here
    
    while (!(cin >> data) || cin.get() != '\n')
    {
      cin.clear();
      cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
      // error message and re-prompt here
    }
    Both those solutions allow mixing of cin >> and getline.

    If you want it all encapsulated in some functions, see this post:

    http://cboard.cprogramming.com/showp...4&postcount=24

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    I don't understand the griping about how hard it is to do a certain thing. Is reading a single line of input hard? Maybe for some people -- I don't really think so -- but whether it's hard or not, why not just get it right once, throw it in your personal library "bag of tricks" and forget about it.

    Sure, sometimes stuff is "hard." So write the code and then never write it again.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Sure, sometimes stuff is "hard." So write the code and then never write it again.
    I agree, except that once you've gained another year or five of experience, you can usually come up with a better way to solve the same problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps it is more of a lot of code to do such an easy and common thing. Having to repeat all that code can be tiring. There is "standard" solution available out of the box.
    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.

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Elysia View Post
    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.
    So does cin evidently.

    I thought we all agreed that class you made suffered from feature creep and the better solution was a collection of free functions.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, you rejected the newer features, which I took under advisement, but not the latest working version I posted.
    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.

  7. #22
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So where can this function be found?

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is a class I had made and I wish I knew. I do not have its address an I cannot search for it.
    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.

  9. #24
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, if you want to read user input, until an enter is pressed, and just not want to worry about something going bad, then you can use something like this. Note that this isn't to use exactly how it is, it is just a quick possibly non-working example
    Code:
    bool getlineSafe (istream& is, string& str)
    {
            try 
            {
                  getline(is, str);
                  return true;
            }
            catch()
            {
                  return false;
            }
    }
    If an error occurs you can handle it as you want. Or you can use another method rather than try/catch. The point is that if an error occurs you will need to handle it or ignore it. You don't really have another option.

    Personally I use the simplest function, like get() to get one by one characters and do exactly what I want. Wrap it in a class/function with some parameters and not worry about it again

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I do not know if an empty catch works, though. Does it?
    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.

  11. #26
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_ntua
    Well, if you want to read user input, until an enter is pressed, and just not want to worry about something going bad, then you can use something like this.
    That is not very useful though. By default, std::getline() would not throw an exception due to an I/O error. One might as well just use the return value of std::getline() since it is indirectly convertible to bool.

    Quote Originally Posted by Elysia
    I do not know if an empty catch works, though. Does it?
    I believe it is a syntax error, but that was just an example anyway.
    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. #27
    Registered User
    Join Date
    May 2002
    Posts
    51
    The number of replies confirm the complexity of this. And impatient as i am, I'm moving to Python instead. I never need the low level benefits of C++ anyway.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by finnepower
    The number of replies confirm the complexity of this.
    That is a rather naive measure: some of the replies are merely comments that the process is easier than what some people make it out to be.

    Quote Originally Posted by finnepower
    And impatient as i am, I'm moving to Python instead. I never need the low level benefits of C++ anyway.
    On the other hand, if you can get away with using a higher level language like Python (e.g., your program is I/O intensive rather than CPU intensive), it is usually a good idea to use that instead.
    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

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