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.1Since "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 char610k36 gold badges517 silver badges875 bronze badges
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).
36.9k18 gold badges79 silver badges113 bronze badges
14.1k4 gold badges12 silver badges28 bronze badges
2 Comments
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.456For 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).
495k83 gold badges657 silver badges1.2k bronze badges
Explore related questions
See similar questions with these tags.

