467,915 Members | 1,815 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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
Jul 22 '05 #1
  • viewed: 2456
Share:
2 Replies

"r.magdeburg" <r.*********@arcor.de> wrote in message news:3f***********************@news.freenet.de...
//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.
Jul 22 '05 #2
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;
}

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by david | last post: by
5 posts views Thread by samik_tanik | last post: by
1 post views Thread by mmmgood1 | last post: by
6 posts views Thread by Clint Stowers | last post: by
6 posts views Thread by Rich Raffenetti | last post: by
5 posts views Thread by GarryJones | last post: by
6 posts views Thread by JimmyKoolPantz | last post: by
2 posts views Thread by Pete Forman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.