Std::cout.imbue( std::locale( std::locale( "en_GB.utf8" ), new space_separated ) ) Ĭonst auto stdout_locale = std::cout.getloc() Ĭonst auto& mp_iternational = std::use_facet >( stdout_locale ) Ĭonst auto& mp_brief = std::use_facet >( stdout_locale ) Virtual char do_thousands_sep() const override // in groups of 3 digits Try it out if you would like, and we can see how it works for you. So, any numerical character minus the zero character results in the integer that the character represents: 7 = '7' - '0' Using this method, the algorithm you are trying to build can be defined in a concise main function taking only 13 lines of code ( including separate lines for each curly brace ). Furthermore, chars are actually integers, so we can perform arithmetic on them. Fortunately, this is very simple in c++: int i = c - '0' This works because numbers in the ascii table are sorted in ascending order. The tricky part here is converting each character to an integer. The method is summarized here in pseudocode: set integer sum = 0 Then, in one pass, one could output each digit and accumulate a running sum. In this case, each character can then be interpreted as a digit in and of itself and processed accordingly. Instead, one could store the input as a string. While this is mathematically elegant and very pleasing aesthetically, it is not the optimal solution here. If you do this, you have to split each digit using modular arithmetic.
Split an input number into separate digits.įirst of all, I would not recommend putting your input into an int type. First, you are trying to do two things here: 1. There is actually a very simple solution to this problem.