Get the digit????

This is a discussion on Get the digit???? within the C++ Programming forums, part of the General Programming Boards category; Hello I am writing a fuction to return a number's digit in a spcific plance. Number : 12345 Place:1 = ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Get the digit????

    Hello I am writing a fuction to return a number's digit in a spcific plance.

    Number : 12345
    Place:1 = 5
    Place:2 = 4
    .
    .
    .

    here is what I have:
    Code:
    int get_digit(info& structure,int digit)
    {
    int ret_dig;
    
    ret_dig = structure.d_pop;
    
        
    }
    Please help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Use % and / on a temproary copy of the variable.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    How Would I do this....could you please give me an example?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    12345 % 10 = 5.
    (12345 / 10) % 10 = 4.
    (12345 / 10 / 10) % 10 = 3.
    ... etc.

    % means mod, or remainder (12345 / 10 is 1234 remainder 5, so 12345 % 10 = 5).

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Or convert it to a string and then just use normal array-like indexing [] and some math to get the proper digit.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    Thank You Very Much

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    I think you can write a recursive function for this purpose..

    http://www.cprogramming.com/tutorial/lesson16.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error msg instead of the result so far
    By maybabier in forum C Programming
    Replies: 25
    Last Post: 03-20-2009, 03:45 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 04:28 AM
  4. newbie programmer - needs help bad.
    By hortonheat in forum C Programming
    Replies: 17
    Last Post: 10-20-2004, 06:31 PM
  5. Roman number converter
    By BalanusBob in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 07:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21