outputing digits of an int...

The title says it all...

I have a feeling it is a darn easy thing to do but i can't think of a way at the moment...

so input might be 689504 i need to output 6 8 9 5 0 4.

thanx
edit: below, use snprintf(), to prevent accessing beyond the end of C string!

the two ways that come to mind is either using sprintf() to dump it into a standard C array, or use a C++ stringstream and just use the insertion operator. if you need spaces, you can then just manipulate it like you would a normal string to insert the spaces (i.e. from one of the previous, get a C++ string object, then use the std::string::insert() member to insert the spaces). I don't doubt that there is probably an even easier way, but that was just the first thing that came to mind. hope it helps!
Last edited on
thanx for a quick replay Mal...

The thing is i am not allowed to use anything but loops, mod and division functions...(it's a school thing...don't ask me why). So i need to come up with a loop that combines mod and div and outputs digit " " digit...

Last edited on
Here is the step you should follow:
1- read the input
2- make a variable which should be the 10 smallest power greater than the input (if input is 123456, this should be 1000000)
3- iterate a division by 10 of that variable and output the value of the digit ( do your calculations knowing that 123456%1000==456 )
Are you allowed to use recursion?
When i asked my teacher for a hint she only told me mods and divs so recursion is not an option since we didn't get there yet.

Bazzy i did something similar...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if (temp < 10)
           {
           cout << temp;
           }
    else       
    for (i = 4; i>=1; i--)
    {
        if (temp /static_cast<int>(pow(10.00, i)) == 0)
        {
        temp = temp % static_cast<int>(pow(10.00, i));
        
                 continue;
        }         
        else
        {
        cout << temp /static_cast<int>(pow(10.00, i)) << " ";
        temp = temp % static_cast<int>(pow(10.00, i));
             if (temp < 10)
                cout << temp;
                
        }
}


however this can only be applied to integers not greater then 99999...and also this can't print out 0s in ints that have 0 in other places.

Last edited on
You are missing step 2 of my previous post,
modify the else block as follows:
1
2
3
4
5
6
7
...
    else
    {
        for (i = 0; pow(10.0, i)<temp; i++); //increase i until it reaches a value which is bigger than temp
        for (; i>=1; i--) // now you have the right value for i so you needn't an assignment for it
             //...
    }


( Are you shure that you need the first and the second if? )
Last edited on
hmmm that makes sense...if i modify it like that i don't need the ifs.

Thanx i'll try it..
I got it to work...thanx everyone for posting!

Buzzy your previous code needed slight change though...

1
2
3
4
5
6
7
8
9
10
for (i=0; static_cast<int>(pow(10.0,i)) < temp; i++);
    
    
    for(i=i-1;i>=0; i--) // i do need to assign new value for i because previous for loop adds 1 when it evaluates to true last time it runs.     
    {
    cout << temp/(static_cast<int>(pow(10.0,i))) << " ";
    
    temp = temp % (static_cast<int>(pow(10.0,i)));
    
    }    


not a big deal...thanx for help
Last edited on
Topic archived. No new replies allowed.