Question Details

No question body available.

Tags

c++ performance formatting format iostream

Answers (3)

Accepted Answer Available
Accepted Answer
January 15, 2026 Score: 6 Rep: 610,416 Quality: Expert Completeness: 80%

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 
#include 

int main() { std::cout num1; std::cin >> num2;

std::cout
January 15, 2026 Score: 6 Rep: 14,145 Quality: High Completeness: 80%

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 
#include 
#include 

int main() { std::cout > num1; std::cin >> num2;

// C++20 std::cout
January 15, 2026 Score: 2 Rep: 494,690 Quality: Medium Completeness: 60%

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