C++ efficient method of formatting std::cout

1 week ago 12
ARTICLE AD BOX

How would one more cleanly format this while keeping spaces?

I wouldn't print out any hard-coded space characters at all. I would let std::cout pad out the values as needed, via the std::setw and std::setfill stream manipulators, eg:

#include <iostream> #include <iomanip> int main() { std::cout << "Calculator App" << std::endl; double num1, num2; std::cin >> num1; std::cin >> num2; std::cout << std::setfill(' '); std::cout << std::left << std::setw(15) << "First number:" << num1 << std::endl; std::cout << std::left << std::setw(15) << "Second number:" << num2; << std::endl; return 0; }

Output:

Calculator App First number: 5.3 Second number: 81.1

Online Demo

Since "First number:" and "Second number:" are less than 15 characters in length, the fill character ' ' will be output automatically, eg:

+---------------------- 15 width ---------------------------+ | | +---------------------------------------------------------------------------+ | F | i | r | s | t | | n | u | m | b | e | r | : | | | 5 | . | 3 | ⏎ | +---------------------------------------------------------------------------+ ^ ^ fill chars +---------------------- 15 width ---------------------------+ | | +-------------------------------------------------------------------------------+ | S | e | c | o | n | d | | n | u | m | b | e | r | : | | 8 | 1 | . | 1 | ⏎ | +-------------------------------------------------------------------------------+ ^ fill char

Remy Lebeau's user avatar

1 Comment

This is the way. But a paranoia since the asker is learning online with an automated grading tool, a judge: Online judges are picky sons of <expletive deleted>s. If the judge expects one space and you give it anything but one space, the judge rejects your program. Sometimes it'll be kind and highlight the discrepancy, but a lot of judges give no hints at all. Read the program expectations carefully and make sure you deliver the expected output exactly

2026-01-15T20:41:27.81Z+00:00

Switch to a version of C++ that supports std::print or use C++20's std::format.

Which will provide much cleaner and readable syntax (std::print might even be a bit more efficient at runtime)

And finally if you want to learn C++, don't do it by online judge/competitive coding.
Find a good book, visit https://learncpp.com/. And you might want to have a look at the
C++ core guidelines.
So you can at least compare your training material against how C++ actually should be used
(most training material just is outdated).

#include <iostream> #include <format> #include <print> int main() { std::cout << "Calculator App\n"; double num1, num2; std::cin >> num1; std::cin >> num2; // C++20 std::cout << std::format("{:<15}{}\n", "First number:", num1); std::cout << std::format("{:<15}{}\n", "Second number:", num2); // C++23 std::print("{:<15}{}\n", "First number:", num1); std::print("{:<15}{}\n", "Second number:", num2); return 0; }

wohlstad's user avatar

wohlstad

36.9k18 gold badges79 silver badges113 bronze badges

Pepijn Kramer's user avatar

2 Comments

[C++ core guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) and friends look a lot like gooned links, but the markdown that generates them looks very deliberate. Best corrected by you if it is indeed a mistake.

2026-01-15T21:35:55.917Z+00:00

I saw the comment above after I already edited... sorry if the original format was intended (you can rollback in that case).

2026-01-16T05:21:43.523Z+00:00

It's hard to guess whether it applies in this specific case, but when you need specific formatting in general, it can be useful to wrap the data up into a class, and overload operator<< for that class to assure consistent formatting. But we also want it to be easy to use, so in this case, we may want a user defined literal operator to keep things neat and tidy, so to speak:

#include <iostream> #include <iomanip> #include <string_view> class Label { std::string_view label; public: Label(char const *s, std::size_t len) : label(s, len) {} friend std::ostream &operator<<(std::ostream &os, Label l) { return os << std::left << std::setw(15) << l.label; } }; Label operator""_L(char const *s, std::size_t len) { return Label(s, len); } int main() { double num1, num2; std::cin >> num1; std::cin >> num2; std::cout << "Calculator App\n"; std::cout << "first number:"_L << num1 << '\n'; std::cout << "second number:"_L << num2 << '\n'; }

The operator""_L allows use to use _L as a suffix on a string literal to create a Label object, which overloads operator<< to use the formatting we want, so our result will come out like we want:

1.234 3.456 first number: 1.234 second number: 3.456

For a real calculator app, we might (easily) want to use a class with overloaded operator<< for each operand as well, so we print the numbers out consistently formatted as well (but in this case, we probably don't need/want the user defined literal operator).

Jerry Coffin's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article