I want to be able to extract separate digits from a number.

the title says it all Lets say the year is 1981 then i want to be able to put the digits 1,8,9 and 1 in a variable, then I also want to be able to put the whole number in an variable. ... How can I do this?
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
#include<iostream>
#include<cstdlib>
using namespace std;
main()
{
    int num1[10], num=0, x;
    do
    {
        cout<<"Enter number from 1 to 1000000: "<<"\n";
        cin>>num;
        if(!cin)
        {
            cout<<"Invalid Input! Try Again\n";
            cin.clear();
            cin.ignore();
        }
    }while(!cin);
    do
    {
        for(x=0;x<10;x++)
        {
            num1[x]=num/10;
        }
    }while(num>10);
        cout<<" "<<x;
        system("pause");
}

So far this is my code for this program
closed account (o3hC5Di1)
Hi there,

You can try to read the input from the user into a string, that will allow you to access every single digit easily.
As for converting strings to ints, see: http://www.cplusplus.com/forum/articles/9645/

Hope that helps.

All the best,
NwN
thanks for the reply but that topic is too complicated for me i'm just learning...so i need a more simple explanation...
If you use the type int for processing of numbers then it has its own characteristics as maximum and minimum values, the number of decimal digits and so on. So insetad of magic values 10 and 1000000 it is better to use predefined values for the type int. These values can be gotten if to include header <limits>
So your program could look the following way

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
#include<iostream>
#include<cstdlib>
#include <limits>

using namespace std;

int main()
{
    const int base = 10;
    int digits[numeric_limits<int>::digits10 + 1];
    int num;

    do
    {
        cout << "Enter number from 0 to " << numeric_limits<int>::max() << ":  ";
        cin >> num;
        if ( !cin )
        {
            cout<<"Invalid Input! Try Again\n";
            cin.clear();
            cin.ignore();
        }
    } while ( !cin );

    int i = 0;
    do
    {
            digits[i++] = num % base;
    } while ( num /= base );

     cout << "i =  " << i << endl;

     system("pause");

     return 0;
}


I did not test it but hope it must work.
Last edited on
closed account (o3hC5Di1)
Hi there,

1
2
3
4
5
6
7
8
9
10
char input[10]; //string of maximum 10 characters

cin >> input; //store the user input in the string, for instance 12345

//now input[0] = 1, input[1] = 2, input[2] = 3, etc.

//to get the string as a number:

int number;
number = atoi(input);  //atoi() function converts a c-string to an integer 


Hope that's clearer for you.

All the best,
NwN

Last edited on
now i get it but will this help me will my project?
will this extract every number on the input...but thanks for explaining it more simple for me
@ mr.vlad its not working...but thank for your insight...
to make it correct i want to make the digits a variable 1, 9, 8 and 1

@maculhet
@ mr.vlad its not working...but thank for your insight...


I wonder why does not it work? What is the problem?
Last edited on
its working now...and its counting every digit off my input thanks...now all i need to do is to figure out how to display every number that has been extracted...
closed account (o3hC5Di1)
@vlad - thanks very much for that tip on <limits> :)

All the best,
NwN
@maculhet
its working now...and its counting every digit off my input thanks...now all i need to do is to figure out how to display every number that has been extracted...


Did you mean "every digit"?

As you know how many there are filled elements in the array due to the value in 'i', you can output the array in the reverse order

1
2
while ( i-- != 0 ) cout << digits[i] << ' ';
cout << endl;
Topic archived. No new replies allowed.