How to read a part of an integer

Discussion in 'C++' started by panther, May 3, 2007.

  1. panther

    panther Guest

    Hi,

    I need to read part of an integer by another variable.
    As example,
    if user enter 761120921,
    then, i = 761120921
    j = 76
    k = 112
    l = 0921 as that.
    can you explain how do i acheive this?
     
    panther, May 3, 2007
    #1
    1. Advertising

  2. panther

    Zeppe Guest

    panther wrote:
    > Hi,
    >
    > I need to read part of an integer by another variable.
    > As example,
    > if user enter 761120921,
    > then, i = 761120921
    > j = 76
    > k = 112
    > l = 0921 as that.
    > can you explain how do i acheive this?
    >


    you need to split the number manually. At first, do you need 0921 or 921
    as a last number? because if you don't want to loose the zero you have
    to work at a string level. If you want to loose the zero, and you just
    want to work generally with integers, you can just for example take the
    last four digits by doing

    i % 10000;

    and then go for the high order digits by

    i /= 10000;

    Regards,

    Zeppe
     
    Zeppe, May 3, 2007
    #2
    1. Advertising

  3. panther

    seank76 Guest

    On May 3, 12:53 pm, panther <> wrote:
    > Hi,
    >
    > I need to read part of an integer by another variable.
    > As example,
    > if user enter 761120921,
    > then, i = 761120921
    > j = 76
    > k = 112
    > l = 0921 as that.
    > can you explain how do i acheive this?


    panther's response is the right way to do it.
    However, if you simply need to turn the integer into a string, you
    could use itoa(int) or a some other comparable functions.
     
    seank76, May 3, 2007
    #3
  4. panther

    Marcus Kwok Guest

    panther <> wrote:
    > I need to read part of an integer by another variable.
    > As example,
    > if user enter 761120921,
    > then, i = 761120921
    > j = 76
    > k = 112
    > l = 0921 as that.
    > can you explain how do i acheive this?


    One way would be to use the % and / operators. For example,

    i = 761120921
    j = i / 10000000
    k = (i / 10000) % 1000
    l = i % 1000

    Another way would be to read the number in as a string, and use the
    string::substr() function.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, May 3, 2007
    #4
  5. panther

    Marcus Kwok Guest

    Marcus Kwok <> wrote:
    > panther <> wrote:
    >> I need to read part of an integer by another variable.
    >> As example,
    >> if user enter 761120921,
    >> then, i = 761120921
    >> j = 76
    >> k = 112
    >> l = 0921 as that.
    >> can you explain how do i acheive this?

    >
    > One way would be to use the % and / operators. For example,
    >
    > i = 761120921
    > j = i / 10000000
    > k = (i / 10000) % 1000
    > l = i % 1000


    Actually it should be l = i % 10000 if you wanted the last 4 digits.

    > Another way would be to read the number in as a string, and use the
    > string::substr() function.


    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, May 3, 2007
    #5
  6. panther

    Default User Guest

    seank76 wrote:

    > On May 3, 12:53 pm, panther <> wrote:
    > > Hi,
    > >
    > > I need to read part of an integer by another variable.
    > > As example,
    > > if user enter 761120921,
    > > then, i = 761120921
    > > j = 76
    > > k = 112
    > > l = 0921 as that.
    > > can you explain how do i acheive this?

    >
    > panther's response is the right way to do it.
    > However, if you simply need to turn the integer into a string, you
    > could use itoa(int) or a some other comparable functions.


    That is a non-standard function, and therefore is platform-specific and
    off-topic in this group. There are a number of ways to accomplish this
    with resorting to such things.

    The best idea would be to use std::string and the capabilities from
    sstream.



    Brian
     
    Default User, May 3, 2007
    #6
  7. panther

    Jim Langston Guest

    "panther" <> wrote in message
    news:...
    > Hi,
    >
    > I need to read part of an integer by another variable.
    > As example,
    > if user enter 761120921,
    > then, i = 761120921
    > j = 76
    > k = 112
    > l = 0921 as that.
    > can you explain how do i acheive this?


    It depends. It sounds to me like this is some type of part number with
    different positions specifying different things. I once did this in a
    retail system and realized when they ran out of space in the 2 byte int that
    I should of use strings so they could use characters.

    Anyway, it depends mostly becuase you said "if user enters..." which means
    you are dealing with user input. I find the best way to deal with user
    input is by characters, by strings. Once you get it in a string you can see
    exactly what they entered.

    That is, what if they etenered
    071120921
    when that gets put into an int it becomes
    71120921
    And now you have more of a headache, especially if you have different lenght
    numbers they can enter. So, I would accept their input into a string.
    Validate that all characters are numeric. Break up the characters into the
    parts I want, load them in.

    Untested pseudo type code:

    int StrToInt( const std::string& Number )
    {
    std::stringstring Stream;
    Stream << Number;
    int Result;
    Stream >> Result;
    return Result;
    }

    std::string Number;
    while ( std::cin >> Number && Number != "" )
    {
    if ( Number.length() != 9 )
    {
    std::cout << "Input must be 9 digits. Try again.\n";
    break;
    }
    if ( ! isdigits( Number ) )
    {
    std::cout << "Only numbers 0-9 are allowed. Try again.\n";
    break;
    }
    // at this point we know we have 9 numberic characters in Number
    int j = StrToInt( Number.substr( 0, 2 ) );
    int k = StrToInt( Number.substr( 2, 3 ) );
    int l = StrToInt( Number.substr( 5, 4 ) );

    // Do whatever with j, k and l here as integers
    }

    If you aren't dealing directly with user input but are receiving an int as
    input, then I've just been blowing hot air :D

    The general rule is: when dealing with user input treat it as
    characters/strings as long as possible to validate it. That's the rule I
    follow anyway.
     
    Jim Langston, May 5, 2007
    #7
  8. panther

    panther Guest

    On May 6, 3:42 am, "Jim Langston" <> wrote:
    > "panther" <> wrote in message
    >
    > news:...
    >
    > > Hi,

    >
    > > I need to read part of an integer by another variable.
    > > As example,
    > > if user enter 761120921,
    > > then, i = 761120921
    > > j = 76
    > > k = 112
    > > l = 0921 as that.
    > > can you explain how do i acheive this?

    >
    > It depends. It sounds to me like this is some type of part number with
    > different positions specifying different things. I once did this in a
    > retail system and realized when they ran out of space in the 2 byte int that
    > I should of use strings so they could use characters.
    >
    > Anyway, it depends mostly becuase you said "if user enters..." which means
    > you are dealing with user input. I find the best way to deal with user
    > input is by characters, by strings. Once you get it in a string you can see
    > exactly what they entered.
    >
    > That is, what if they etenered
    > 071120921
    > when that gets put into an int it becomes
    > 71120921
    > And now you have more of a headache, especially if you have different lenght
    > numbers they can enter. So, I would accept their input into a string.
    > Validate that all characters are numeric. Break up the characters into the
    > parts I want, load them in.
    >
    > Untested pseudo type code:
    >
    > int StrToInt( const std::string& Number )
    > {
    > std::stringstring Stream;
    > Stream << Number;
    > int Result;
    > Stream >> Result;
    > return Result;
    >
    > }
    >
    > std::string Number;
    > while ( std::cin >> Number && Number != "" )
    > {
    > if ( Number.length() != 9 )
    > {
    > std::cout << "Input must be 9 digits. Try again.\n";
    > break;
    > }
    > if ( ! isdigits( Number ) )
    > {
    > std::cout << "Only numbers 0-9 are allowed. Try again.\n";
    > break;
    > }
    > // at this point we know we have 9 numberic characters in Number
    > int j = StrToInt( Number.substr( 0, 2 ) );
    > int k = StrToInt( Number.substr( 2, 3 ) );
    > int l = StrToInt( Number.substr( 5, 4 ) );
    >
    > // Do whatever with j, k and l here as integers
    >
    > }
    >
    > If you aren't dealing directly with user input but are receiving an int as
    > input, then I've just been blowing hot air :D
    >
    > The general rule is: when dealing with user input treat it as
    > characters/strings as long as possible to validate it. That's the rule I
    > follow anyway.


    Hi,

    Thanks for all of you. I get the idea Jim. I beleive that is the way I
    have to follow. Because i have to face the situations as you explain
    their.
    Again Thanks for you all.
     
    panther, May 6, 2007
    #8
  9. panther

    panther Guest

    Hi,
    now i have a new problem.
    Here is my codes.

    using namespace std;


    int main()
    {

    string number,number1,number2,number3;


    cout << "Please enter your National ID Number :";
    cin >> number;


    while (number != "" ){

    if (number.length() != 9)
    {
    cout <<"Input must be 9 digits. Try again.\n ";
    break;
    }

    // if (! isdigit(number) )
    // {
    // cout <<" Only numbers 0-9 are allowed. Try again\n";
    // break;
    // }



    else
    {

    number1 = number.substr(0,2);
    number2 = number.substr(2,3);
    number3 = number.substr(5,4);


    int i = atoi(number1.c_str());
    int j = atoi(number2.c_str());
    int k = atoi(number3.c_str());
    cout <<i<<endl;
    cout <<j<<endl;
    cout <<k<<endl;
    break;
    }
    }

    return 0;

    }

    The problem is isdigit() not work with the string. It for the
    character. So how do i check the contain of the string?
    Please help me.
     
    panther, May 8, 2007
    #9
  10. panther wrote:
    > Hi,
    > now i have a new problem.
    > Here is my codes.
    >
    > [..]
    > The problem is isdigit() not work with the string. It for the
    > character. So how do i check the contain of the string?


    Two ways. One is to enumerate all characters and call 'isdigit' for
    each of them. The other is to try to convert the string into some
    large enough number. If it succeeds, you got numbers only.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 8, 2007
    #10
  11. panther

    Default User Guest

    panther wrote:

    > Hi,
    > now i have a new problem.
    > Here is my codes.


    Don't use tabs for indents.

    > using namespace std;
    >
    >
    > int main()
    > {
    >
    > string number,number1,number2,number3;
    >
    >
    > cout << "Please enter your National ID Number :";
    > cin >> number;
    >
    >
    > while (number != "" ){


    What do you think this while() does? How will number ever change?

    >
    > if (number.length() != 9)
    > {
    > cout <<"Input must be 9 digits. Try again.\n ";
    > break;
    > }


    How will the user "try again"? Restart the entire program? That doesn't
    seem very useful.

    > // if (! isdigit(number) )
    > // {
    > // cout <<" Only numbers 0-9 are allowed. Try again\n";
    > // break;
    > // }
    >
    >
    >
    > else
    > {
    >
    > number1 = number.substr(0,2);
    > number2 = number.substr(2,3);
    > number3 = number.substr(5,4);
    >
    >
    > int i = atoi(number1.c_str());
    > int j = atoi(number2.c_str());
    > int k = atoi(number3.c_str());
    > cout <<i<<endl;
    > cout <<j<<endl;
    > cout <<k<<endl;
    > break;
    > }
    > }
    >
    > return 0;
    >
    > }
    >
    > The problem is isdigit() not work with the string. It for the
    > character. So how do i check the contain of the string?


    A string is composed of a group of characters, in sequential order. You
    have a testing function that takes one character. Hmmmm, what should we
    do? Perhaps, iterate over the string, checking each character? How
    would you do that?




    Brian
     
    Default User, May 8, 2007
    #11
  12. panther

    Jim Langston Guest

    "panther" <> wrote in message
    news:...
    > Hi,
    > now i have a new problem.
    > Here is my codes.
    >
    > using namespace std;
    >
    >
    > int main()
    > {
    >
    > string number,number1,number2,number3;
    >
    >
    > cout << "Please enter your National ID Number :";
    > cin >> number;
    >
    >
    > while (number != "" ){
    >
    > if (number.length() != 9)
    > {
    > cout <<"Input must be 9 digits. Try again.\n ";
    > break;
    > }
    >
    > // if (! isdigit(number) )
    > // {
    > // cout <<" Only numbers 0-9 are allowed. Try again\n";
    > // break;
    > // }
    >
    >
    >
    > else
    > {
    >
    > number1 = number.substr(0,2);
    > number2 = number.substr(2,3);
    > number3 = number.substr(5,4);
    >
    >
    > int i = atoi(number1.c_str());
    > int j = atoi(number2.c_str());
    > int k = atoi(number3.c_str());
    > cout <<i<<endl;
    > cout <<j<<endl;
    > cout <<k<<endl;
    > break;
    > }
    > }
    >
    > return 0;
    >
    > }
    >
    > The problem is isdigit() not work with the string. It for the
    > character. So how do i check the contain of the string?
    > Please help me.


    Untested psuedo code again.

    bool IsStringDigits( const std::string& Input ) // If isdigit doesn't
    accept const char, remove const.
    {
    for ( size_t i = 0; i < Input.length(); ++i )
    if ( ! isdigit( Input ) )
    return false;
    return true;
    }
     
    Jim Langston, May 9, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,987
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    542
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Alex Vinokur

    Integer part of double

    Alex Vinokur, Jan 16, 2007, in forum: C Programming
    Replies:
    18
    Views:
    593
    Keith Thompson
    Jan 17, 2007
  4. Yin Zhu
    Replies:
    15
    Views:
    980
    Richard Bos
    Sep 12, 2007
  5. Jack
    Replies:
    8
    Views:
    303
Loading...

Share This Page