CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: setw related

  1. #1
    Join Date
    May 2015
    Posts
    500

    setw related

    I am trying to output some info and in the legacy code, they used setw. I am trying to match that..

    Code:
            std::ostringstream os;
    
            os << "Name    QualityIndicator       MaxUlBitRate           MaxDlBitRate            nGuarUlBitRate          nGuarDlBitRate" << std::endl;
    
    		for (; itr != end; ++itr)
    		{
    			Data = *p_itr;
    
    			os << p_itr.sName << setw(11) << p_itr.nQualityIndicator << setw(30) << p_itr.nMaxUlBitRate
    					   << setw(53) << p_itr.nMaxDlBitRate << setw(78) << p_itr.nGuarUlBitRate
    					   << setw(102) << p_itr.nGuarDlBitRate << endl;
    		}
    Is there any suggestions to make this better..I somehow feel setw is not the choice, but may be the legacy code chose this for some reason..May be because i am not a c++ expert, not able to see it.

    thanks a lot
    ~p
    Last edited by pdk5; November 8th, 2015 at 05:52 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: setw related

    itr is being incremented within the for loop but is not being used within the loop. Also Data is being assigned within the loop but again not used. p_itr is suggesting it is a pointer but is not changed within the loop and is not being accessed as a pointer

    How is p_itr related to itr? What is end? Where is itr initialised? What is the type of itr and p_itr?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: setw related

    @kaud: sorry it was my mistake, I had tried to just show the kind of psedocode ..

    I will add the almost the code now I have changed and works fine.

    Code:
    Responses::Pc::List const& pcList = dynamic_cast<Responses::Pc::List const&>(msg);
            os << "TitleaName                  Qua       MaxUlBitRate           MaxDlBitRate            nGuarUlBitRate          nGuarDlBitRate" << std::endl;
    
    		std::vector<Data::Pc::Data>::const_iterator itr = pcList.data.dataItems.begin();
    		std::vector<Data::Pc::Data>::const_iterator end = pcList.data.dataItems.end();
    
    		for (; itr != end; ++itr)
    		{
    			OAM::Data::Pc::Data pcData = *itr;
    
    	        os << left << setw(28) << pcData.sTitleaName  << setw(10) << pcData.nQua << setw(23)<<pcData.nMaxUlBitRate
    	        		<<setw(24)<< pcData.nMaxDlBitRate<<setw(24)<<pcData.nGuarUlBitRate<<setw(22)<<pcData.nGuarDlBitRate << endl;
    
    		}

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: setw related

    Seems good to me. If you are using cout, you don't have much of a choice, and need to use stream formatting anyways. It's not pretty, but it does work and is pretty robust. I'd leave the code as-is unless you have a good reason to touch it. All it it doing is printing numbers left aligned to certain widths. Though arguably, the last setw is not needed. Also still arguably, the original code *could* have considered using a more robust tab separation scheme, or better yet, csv-like scheme. I don't have the context.

    If you *do* want to put effort into upgrading your app's stream and string formatting facilities, consider using instead The Boost Format library. In particular, you *could* be interested in its absolute tabulation scheme. The equivalent code would look like this:

    cout << format("%s %|28t|%s %|38t|%s" %|71t|%s %|95t|%s %|119t|%s \n")
    % pcData.sTitleaName % pcData.nQua % pcData.nMaxUlBitRate % pcData.nMaxDlBitRate % pcData.nGuarUlBitRate % pcData.nGuarDlBitRate;

    Some differences you'll notice here is that this format guarantees at least 1 space between each word. Furthermore, if a "word" goes past its width boundaries, the following words will not be padded, and hopefully, re-align after a few columns.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    May 2015
    Posts
    500

    Re: setw related

    Thanks a lot Monarch for the inputs.

    Yes I will try going through the boost format , thanks a lot for the link

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured