473,545 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
2 2684

"r.magdebur g" <r.*********@ar cor.de> wrote in message news:3f******** *************** @news.freenet.d e...
//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(nume ric_limits<std: :streamsize>::m ax(),'\n'); // call lifted from Josuttis The C++ Standard Library, pg. 609
return 0;
}

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13760
by: david | last post by:
Hi, I have an application as follows: MySQL database Back-Eend linked to MS Access Front-End and ASP Web Application. I require users to enter Serial Numbers such as: 0105123567 (10 digits), the first 4 being the month and year (mmyy)
5
11490
by: samik_tanik | last post by:
I need to export a datagrid to Excel. I could did this. But, also need to keep the leading zeros in the data. How can I acheive this? Any help would be appreciated. -- Thanking you in anticipation, Regards,
1
4541
by: mmmgood1 | last post by:
Help, I'm linking an excel spreadsheet in access and I have datafields with leading zeros (01021). When the file is linked in access, I get a #num in the field with the leading zeros. The zeros are needed. I've formated the field to be a text field, general number, and number and still I get the #num! error, or the leading zero is dropped. ...
6
5292
by: Clint Stowers | last post by:
Using A2k Exporting a query to a CSV file. The problem is any text fields (i.e. 000345) lose any leading zeros. Exporting to an excel file this problem does not exist. Tried to create a SpecificationName via the Export Wizard without success. Obviously doing something wrong.
6
19140
by: Rich Raffenetti | last post by:
How can one format an integer into a hex string with leading zeros? Suppose an integer is 512 which in Hex is 200. I wish to print the 4-byte integer as 0200 or even 0x0200. The HEX function doesn't create leading zeros. The Format function (format(value,"X") doesn't create leading zeros. I believe there should be a simple way that...
5
3467
by: GarryJones | last post by:
I have code numbers in 2 fields from a table which correspond to month and date. (Month, Code number) Field name = ml_mna 1 2 3 etc up to 12 (Data is entered without a leading zero)
6
7738
by: JimmyKoolPantz | last post by:
Task: Customer wants a script of the data that was processed in a "CSV" file. Problem: Zip-Code leading zeros are dropped Basically we have a client that has requested a custom script for each file that he has us process. He wants this in a Comma Delimited Format.
0
4078
by: Monty | last post by:
Hi All, I am having a problem with leading zeros being stripped from fields in a CSV file when I bring them in using Jet/OleDB. In VB.Net/VS 2008, I am accessing a CSV file like so: sSQL = "SELECT * FROM " sConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _ Microsoft.VisualBasic.FileIO.FileSystem.GetParentPath(msFile) & _
2
7060
by: Pete Forman | last post by:
I'm holding off installing Python 2.6, waiting for some packages to become available for it. I wonder if someone could tell me the best way to avoid future problems parsing decimal integers with leading zeros. 9 That works in 2.5 but will break in 2.6 AFAIK as int() is being changed to use Numeric Literal syntax. It will give a syntax...
0
7487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.