[RESOLVED] How to pad a string with trailing zeros using ostringstream
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: [RESOLVED] How to pad a string with trailing zeros using ostringstream

  1. #1
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    [RESOLVED] How to pad a string with trailing zeros using ostringstream

    Hi,

    The code that I have does not work. Could some help -- thanks.

    Code:
    
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    
    int main()
    {
            std::string str3 = "99W8";
            std::ostringstream ss4;
            ss4 << str3 << std::setw(7) << std::setfill('0');
            std::string s4(ss4.str());
            std::cout << s4 << std::endl;
            return 0;
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,388

    Re: How to pad a string with trailing zeros using ostringstream

    define "does not work"
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: How to pad a string with trailing zeros using ostringstream

    the output:
    99W8

    I would like it to be:
    99W8000

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,365

    Re: How to pad a string with trailing zeros using ostringstream

    Code:
    ss4 << str3 << std::setw(7 - str3.length()) << std::setfill('0') << "";
    After you set the width, you need to output something.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Jan 2012
    Location
    USA
    Posts
    91

    Re: How to pad a string with trailing zeros using ostringstream

    That's slick, @2kaud, thank you.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center