leading zeros - wrong int

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • r.magdeburg

    leading zeros - wrong int

    //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


  • Ron Natalie

    #2
    Re: leading zeros - wrong int


    "r.magdebur g" <r.magdeburg@ar cor.de> wrote in message news:3fcb8c05$0 $25763$9b622d9e @news.freenet.d e...[color=blue]
    > //please tell me why...
    > //and give me a hint to solve the problem with leading zeros.
    >[/color]
    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.


    Comment

    • red floyd

      #3
      Re: leading zeros - wrong int

      r.magdeburg wrote:
      [color=blue]
      > //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
      >
      >[/color]

      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(nume ric_limits<std: :streamsize>::m ax(),'\n'); // call lifted from Josuttis The C++ Standard Library, pg. 609
      return 0;
      }

      Comment

      Working...