fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <locale>
  4. struct space_out : std::numpunct<char> {
  5. char do_thousands_sep() const { return ' '; } // separate with spaces
  6. std::string do_grouping() const { return "\1"; } // groups of 1 digit
  7. };
  8. int main()
  9. {
  10. int n;
  11. while(!( std::cout << "Enter a three-digit integer: "
  12. && std::cin >> n
  13. && n > 99 && n < 1000))
  14. {
  15. std::cin.clear();
  16. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  17. std::cout << "This was not a three-digit integer, try again\n";
  18. }
  19.  
  20. std::cout.imbue(std::locale(std::cout.getloc(), new space_out));
  21. std::cout << n << '\n';
  22. }
  23.  
Success #stdin #stdout 0s 3036KB
stdin
549
stdout
Enter a three-digit integer: 5 4 9