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!
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...
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 )
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 tempfor (; 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? )
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)));
}