-
December 29th, 2013, 02:35 PM
#1
[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;
}
-
December 29th, 2013, 02:54 PM
#2
Re: How to pad a string with trailing zeros using ostringstream
Victor Nijegorodov
-
December 29th, 2013, 02:58 PM
#3
Re: How to pad a string with trailing zeros using ostringstream
the output:
99W8
I would like it to be:
99W8000
-
December 29th, 2013, 03:03 PM
#4
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. 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)
-
December 29th, 2013, 03:06 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|