Prompt the user for a six digit integer then output each digit of the number.

Well so far this is the code I have :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int inputnum,changenum;
int nr = 0;
int math = 1;
int parameter;

int main()
{
    cout<<"Enter any number"<<endl;

    cin>>inputnum;
    changenum=inputnum;


    for( int c=0; c<6;c++)
    {
    	nr=nr+1;
        inputnum=inputnum/10;
    }
    for (int i=1;i<nr;i++)
    {
    	math=math*10;
    }
    while (changenum!=0)
    {
    	parameter=changenum/math;
        changenum=changenum%math;
        math=math/10;
        cout<<parameter<<endl;
    }

    return 0;
}


It works fine with a test input of 123456, but when you input 600000 it cuts off the zeros. Although it does work 600001.

Any ideas on how to fix this?

Thanks
I think it would be easier to simply use a std::stringstream/std::string combination to get the number into text format. Unless you have to use modulus/division, anyway.

Anyway...lines 18-22 don't seem to do anything other than make nr = 6 and divide input num by 10 6 times. Then the next loop just multiplies math by 10 5 times. Your variable names are kind of vague, I'm not really sure how "changenum" and "math" are supposed to be related until I read where they are used.

As for the problem, it's that after the first while loop, changenum % math will give you 0, so the loop will exit.
I think...if you mod the number by 10 and then output this remainder you have completed step #1
If you then divide the number by 10 and then mod the result by 10 and output tis remainder you will have completed the second step. Repeat 4 more times.
Topic archived. No new replies allowed.