trying to pull a specific digit from an intiger

Hello,

I have been trying to figure out how to cout a specific digit from an integer and I am not quite sure where to begin. I have been reading over guides for using count functions, rfind functions, and they don't appear to be quite what I am looking for. What I am attempting to do is enter a variable:

1
2
3
4
5
6
7
    cout << "Enter a value for A. The value must be 0 to 999999999: ";
        cin >> A;
        cin.ignore();

        cout << "Enter a second value for A. This value must be 1 to 9: "; 
        cin >> D;
        cin.ignore();


and get it to read from left to right the specific digit placement.

Ex: value: 96538413
Digit 3: 5
Digit 4: 3

This is what my code looks like so far, and I have everything working except this one part. I am mainly just looking for a direction to go in on how to get this last bit working. I would greatly appreciate any tips on this matter.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using namespace std;

int main()
{

        long A;
        long B;
        long C;
        long D;
        long E;
        long F;
        
        cout << "Enter a value for A. The value must be 0 to 999999999: "; // Primary cin A
        cin >> A;
        cin.ignore();

        cout << "Enter a second value for A. This value must be 1 to 9: "; // secondary cin A, use for digit count
        cin >> D;
        cin.ignore();

        cout << "Enter a value for B. The value must be 0 to 999999999: "; // Primary cin B
        cin >> B;
        cin.ignore();

        cout << "Enter a second value for B. This value must be 1 to 9: "; // Secondary cin B
        cin >> E;
        cin.ignore();

        cout << "Enter a value for C. The value must be 0 to 999999999: "; // Primary C
        cin >> C;
        cin.ignore();

        cout << "Enter a second value for C. This value me be 1 to 9: "; // Secondary C
        cin >> F;
        cin.ignore();

        if ( 0 >= A < 1000000000, 0 > B < 10)
        {
           cout << "Value " << A << " is " << (ceil(log10(A))) << " digits, and digit " << D << " is " << "placeholder" << endl;

           cout << "Value " << B << " is " << (ceil(log10(B))) << " digits, and digit " << E << " is " << "placeholder" << endl;

           cout << "Value " << C << " is " << (ceil(log10(C))) << " digits, and digit " << F << " is " << "placeholder"+ << endl;

           cout << "Digital Total:" << ((ceil(log10(A))) + (ceil(log10(B))) + (ceil(log10(C)))) << endl;
        }



system("PAUSE");
return 0;
}
Why not read it into a string rather than an integer?
Hi!

I'm new to all this too... just started learning C++ recently but I think you might be able to use an int array for A[9].

In this way you can access individual components of the 9 digit number using A[i], where i is the position of the number.

Cheers!
Unicyclist
As kbw said, read it into a std::string and just loop through.

1
2
3
4
5
6
std::string str = "123456789";
std::string::iterator it;
int i = 1;

for(it=str.begin();it<str.end();it++)
    std::cout <<"Digit " << i++ << ": " << *it << '\n';


A string can be thought of as an array of characters. And since you're accessing the characters in left to right order, the index of each character matches exactly what you're looking for.

For example, given:
std::string str = "A fat dog";

Then str[0] is A, str[2] is f and so on. mcleano's loop above will print the characters and their positions for you.
Thank you for the explanation on strings. I was thinking using a string, but I was totally unsure on how they functioned. Everywhere I had looked was pretty unclear.
Topic archived. No new replies allowed.