Velocity Reviews > C++ > leading zeros - wrong int

r.magdeburg
Guest
Posts: n/a

 12-01-2003
//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;
}
//input 0045 screen output 37
//input 0049 output 4
// 094 0
//and so on
//thank you

Ron Natalie
Guest
Posts: n/a

 12-01-2003

"r.magdeburg" <(E-Mail Removed)> wrote in message news:3fcb8c05\$0\$25763\$(E-Mail Removed)...
> //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.

red floyd
Guest
Posts: n/a

 12-01-2003
r.magdeburg wrote:

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

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Pete Forman Python 2 11-12-2008 01:04 PM Dmitry Bond. ASP .Net 1 10-13-2005 08:55 AM Hal Styli C Programming 14 01-20-2004 10:00 PM Schnoffos C Programming 2 06-27-2003 03:13 AM Luke Zhang [MSFT] ASP .Net 0 06-26-2003 01:34 PM