Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8936549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:14:19+00:00 2026-06-15T10:14:19+00:00

Learning C++ with help of Bruce Eckel Thinking in C++. Stuck in exercise 05

  • 0

Learning C++ with help of Bruce Eckel “Thinking in C++”. Stuck in exercise 05 of chapter “Iostreams”:

Text of exercise

We know that setw( ) allows for a minimum of characters read in, but what if you wanted to read a
maximum? Write an effector that allows the user to specify a maximum number of characters to
extract. Have your effector also work for output, in such a way that output fields are truncated, if
necessary, to stay within width limits.

I understand how to create manipulators both without and with parameter (which one is called effectors in the book terminology). But do not understand how to limit maximum number of characters to extract. std::ios_base::width specifies the minimum number of characters.

Shoud I do some tricks with underlying streambuf object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:14:20+00:00Added an answer on June 15, 2026 at 10:14 am

    Its not a perfect solution (but I can’t think of another way at the moment without reading the iostream library).

    Say you manipulator is:

    class MaxFieldSize {/*STUFF*/};
    

    When you write the stream operator(s) you write a slightly funky one that does not return an actual stream (but rather returns a stream with a wrapper around it).

    MaxFieldWdithStream operator<<(std::ostream&, MaxFieldSize const& manip);
    

    Now you overload all the stream operator of this class to truncate their input before returning a normal stream object.

    class MaxFieldWithStream { std::ostream& printTruncatedData(std::string& value);};
    

    Then all you need is the generic overloads:

    template<typename T>
    std::ostream& operator<<(MaxFieldWithStream& mfwstream, T const& value)
    {
        std::stringstream  trunStream;
        trunStream << value;
    
        return mfwstream.printTruncatedData(trunStream.substr(0, mfwstream.widthNeeded));
    }
    // You will probably need another overload for io-manipulators.
    

    I would also add a conversion operator that converts MaxFieldWithStream to std::iostream automatically so that if it is passed to a function it still behaves like a stream (though it will loose its max width property).

    class MaxFieldWithStream
    {
        std::ostream& printTruncatedData(std::string& value);};
        operator st::ostream&() const { return BLABLAVLA;}
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Learning C++ with help of Bruce Eckel Thinking in C++. Stuck in exercise 30.
Learning C++ with help of "Thinking in C++" by Bruce Eckel,stuck in exercise 32,
I'm learning C++ right now using Bruce Eckel's Thinking in C++ and I'm in
I am in a learning way of mysql and want help to know where
I'm learning Ruby right now and I got stuck, maybe you guys can help
I'm learning Haskell in the hope that it will help me get closer to
I've read that learning a low level language can help writing higher level languages
Is there any fun, educational game (maybe a flash game, etc.) that help learning
Still learning WPF....thanks for any help. Is there any way to Refactor this: <ListBox
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.