Thread: got difficulty on this

  1. #1
    Registered User
    Join Date
    Nov 2005

    got difficulty on this

    hi, i have difficulty on this question, can someone please tell me how to do it. ?

    example = nthDigit(12345, 3) will return the number 4..

    the question is :
    int nthDigit(int number, int n)

  2. #2
    Registered User
    Join Date
    Nov 2005
    NOTE this is not school work , just for my own purpose on understanding. thank you

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    You can parse digits with modulus and division. Think about it for a bit. Maybe experiment and see what happens. Then from there you can write the function.
    Last edited by SlyMaelstrom; 02-01-2006 at 08:06 AM.
    Sent from my iPadŽ

  4. #4
    Join Date
    Nov 2004
    or the rough and slow approach of turning it into a string, taking the nth character from that string, and returning that as an int.

    Slow, ugly as hell, but it works.

    And used more often than you think, especially when the original data is stored as strings which is so often the case (anything coming out of networks through http for example).

  5. #5
    Registered User
    Join Date
    Apr 2003
    Heck, you can use stringstreams to do that as well:

    1) Read the number into an ostringstream, which will turn it into a string.

    2) Then you can get the string from the ostringstream using the str() member function.

    3) Then you can pick out the nth digit using array notation, e.g. myStr[3], and you can use one of the string constructors to create a string out of the char you have, e.g. string(1, myStr[3]).

    4) You can use the 1 digit string from 3) to create an istringstream object. Finally, you can read from the istringstream into an int type. Voila! You will have the digit as a numeric type--and without doing any math.
    Last edited by 7stud; 02-01-2006 at 08:44 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. have some difficulty compiling a project
    By Masterx in forum C++ Programming
    Replies: 1
    Last Post: 04-04-2009, 07:49 AM
  2. difficulty with pgband syntax
    By lamikins in forum C Programming
    Replies: 1
    Last Post: 11-02-2008, 01:26 AM
  3. homework giving difficulty
    By jjj in forum C Programming
    Replies: 5
    Last Post: 09-13-2002, 06:35 PM
  4. Game difficulty
    By pdstatha in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2002, 04:12 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 04:06 PM