# Beginner - trying to cout a specific digit from an integer

• 03-15-2010
tidbit
Beginner - trying to cout a specific digit from an integer
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, rfind, strings, and they don't appear to be quite what I am looking for, or I am not understanding them correctly on how to implement the procedure. What I am attempting to do is enter a variable:

Code:

```    cout << "Enter a value for A. The value must be 0 to 999999999: "; // This is the primary value.         cin >> A;         cin.ignore();         cout << "Enter a second value for A. This value must be 1 to 9: "; // This value is used to call the specific digit in the integer.         cin >> D;         cin.ignore();```

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

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

It is mainly the concept behind this little piece of code that I have not been able to grasp. I would greatly appreciate any type of help, or being pointed in the right direction.
• 03-15-2010
EVOEx
You can divide by 10 the number of digits you wish to remove. For instance, for integers:
1234567 / 10 / 10 = 12345

So you divide by 10 twice to remove two digits from the right. Then to get only the rightmost digit, you use modulo 10 (% 10) to get the rest after division by 10, which is the rightmost digit. So
12345 % 10 = 5

Hence:
(1234567 / 10 / 10) % 10 = 5, the 3rd digit from the right, counting from 1.

To get left to right, you first have to get the number of digits. You can do this by dividing by 10 until you get 0. Or you could use log as well, although this might give problems for integer numbers as they take floating-point numbers as parameter.
• 03-15-2010
vart
or convert number to string
Code:

```#include <iostream> #include <string> #include <sstream> int main(int argc, char* argv[]) {         int A = 0, D = 0;         std::cout << "Enter a value for A. The value must be 0 to 999999999: "; // This is the primary value.         std::cin >> A;         std::cin.ignore();         std::cout << "Enter a second value for A. This value must be 1 to 9: "; // This value is used to call the specific digit in the integer.         std::cin >> D;         std::cin.ignore();         std::ostringstream ss;         ss << A;         std::string str = ss.str();         try         {                 std::cout << "Digit " << D << " is " << str[D-1] <<std::endl;         }         catch(...)         {                 std::cout << "Wrong index" << std::endl;         }         return 0; }```
• 03-15-2010
EVOEx
Quote:

Originally Posted by vart
or convert number to string
Code:

```#include <iostream> #include <string> #include <sstream> int main(int argc, char* argv[]) {         int A = 0, D = 0;         std::cout << "Enter a value for A. The value must be 0 to 999999999: "; // This is the primary value.         std::cin >> A;         std::cin.ignore();         std::cout << "Enter a second value for A. This value must be 1 to 9: "; // This value is used to call the specific digit in the integer.         std::cin >> D;         std::cin.ignore();         std::ostringstream ss;         ss << A;         std::string str = ss.str();         try         {                 std::cout << "Digit " << D << " is " << str[D-1] <<std::endl;         }         catch(...)         {                 std::cout << "Wrong index" << std::endl;         }         return 0; }```

I assume you mean str.at(D-1) rather than str[D-1], as operator[] doesn't throw anything on overflow.
• 03-15-2010
vart
Quote:

Originally Posted by EVOEx
I assume you mean str.at(D-1) rather than str[D-1], as operator[] doesn't throw anything on overflow.

Ouch... You assume correct.