Can someone help? :/

Write a full C++ program that inputs three-digit integer, separates the integer into its individual digits and prints the digits separated from one another. For example, if the user types 549, the program should print;
5 4 9
Last edited on
you should provide what u done so far
and not request people at here to help u to do the homework

if like that you cannot learn anything .
Sure, how about this:

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

    std::cout.imbue(std::locale(std::cout.getloc(), new space_out));
    std::cout << n << '\n';
}

online demo: http://ideone.com/mDUf6Q
Last edited on
@Cubbi - just giving the code will not help people learn the language.
@writetonsharma

No, IMO Cubbi is right in a funny way - just imagine what would happen if the OP submitted Cubbi's code for the assignment - LOL 8+D

hehe.... LOL.. you are right.. I didn't look at the code that time.. nice code..!!
A better way of learning ... :D


+1 Cubbi.
Last edited on
Topic archived. No new replies allowed.