Thread: Beginner - trying to cout a specific digit from an integer

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    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.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by vart View Post
    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.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by EVOEx View Post
    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.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying the rightmost digit of a positive number
    By randoms4ever in forum C Programming
    Replies: 6
    Last Post: 10-04-2009, 07:50 AM
  2. Recognizing each digit of an integer
    By sybariticak47 in forum C++ Programming
    Replies: 3
    Last Post: 02-08-2006, 01:23 AM
  3. Need Help with a Bowling Score Program
    By oobootsy1 in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2005, 10:04 AM
  4. Homework due tonight! help please!!
    By Andy717 in forum C++ Programming
    Replies: 41
    Last Post: 04-07-2005, 01:18 PM
  5. Beginner Question about cout and cin
    By Dillius in forum C++ Programming
    Replies: 22
    Last Post: 04-06-2003, 11:17 AM