leading zeros - wrong int

Discussion in 'C++' started by r.magdeburg, Dec 1, 2003.

  1. r.magdeburg

    r.magdeburg Guest

    //please tell me why...
    //and give me a hint to solve the problem with leading zeros.

    //snippet
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    int zahl = 0;
    cout << "Give me an int please: ";
    cin >> zahl;
    cout << "int = " << zahl <<endl;
    getch();
    return 0;
    }
    //examples with leading zeros:
    //input 0045 screen output 37
    //input 0049 output 4
    // 094 0
    //and so on
    //thank you
     
    r.magdeburg, Dec 1, 2003
    #1
    1. Advertising

  2. r.magdeburg

    Ron Natalie Guest

    "r.magdeburg" <> wrote in message news:3fcb8c05$0$25763$...
    > //please tell me why...
    > //and give me a hint to solve the problem with leading zeros.
    >

    When basefield is not set "senses" the base by looking at the leading digits similar to the
    way literal numbers works in the language. Leading zeros cause it to treat
    the base as octal. You can fix it by forcing dec, cin >> dec >> zahl;

    I'm pretty sure the basefield is supposed to be dec by default...I think it's a defect in your
    compiler.
     
    Ron Natalie, Dec 1, 2003
    #2
    1. Advertising

  3. r.magdeburg

    red floyd Guest

    r.magdeburg wrote:

    > //please tell me why...
    > //and give me a hint to solve the problem with leading zeros.
    >
    > //snippet
    > #include <iostream.h>
    > #include <conio.h>
    > int main()
    > {
    > int zahl = 0;
    > cout << "Give me an int please: ";
    > cin >> zahl;
    > cout << "int = " << zahl <<endl;
    > getch();
    > return 0;
    > }
    > //examples with leading zeros:
    > //input 0045 screen output 37
    > //input 0049 output 4
    > // 094 0
    > //and so on
    > //thank you
    >
    >


    Also, iostream.h is deprecated. Use:

    #include <iostream> // new header. Note we don't use conio.h
    #include <limits> // new header. needed for input flush
    using namespace std;
    int main()
    {
    int zahl = 0;
    cout << "Give me an int please: ";
    cin >> zahl;
    cout << "int = " << zahl <<endl;
    cin.ignore(numeric_limits<std::streamsize>::max(),'\n'); // call lifted from Josuttis The C++ Standard Library, pg. 609
    return 0;
    }
     
    red floyd, Dec 1, 2003
    #3
    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. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,111
    Oliver Sturm
    Oct 13, 2005
  2. Luke Zhang [MSFT]

    Re: CSV for Excel - Problem with Leading Zeros

    Luke Zhang [MSFT], Jun 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,262
    Luke Zhang [MSFT]
    Jun 26, 2003
  3. GIMME
    Replies:
    5
    Views:
    43,832
    Dale King
    Feb 13, 2004
  4. Stian
    Replies:
    2
    Views:
    647
    Thomas D'Tak
    Sep 19, 2004
  5. Pete Forman

    int() and leading zeros in Python 2.6

    Pete Forman, Nov 12, 2008, in forum: Python
    Replies:
    2
    Views:
    704
    Pete Forman
    Nov 12, 2008
Loading...

Share This Page