## Problem With 0 At Start of Integer

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Blonk
Posts: 10
Joined: Mon Nov 15, 2010 6:36 pm UTC

### Problem With 0 At Start of Integer

So what I need to do is take an integer such as 03061990 and separate it into a MM/DD/YYYY format. The problem is, when I set an integer's value to something starting with 0, I get a different value, in this case being 32. This may be the case for all values starting with 0, but I won't make assumptions.

Can anyone explain what's happening? If I don't address this situation, 9/12 months aren't able to be calculated. That's a pretty big no-no.

Any help is appreciated

cba
Posts: 72
Joined: Wed Oct 06, 2010 2:58 am UTC
Location: not here...
Contact:

### Re: Problem With 0 At Start of Integer

get the number as a string instead, then just work with that...
For the first time ever! flatland in a mind-boggling 3D!
import antigravity

Blonk
Posts: 10
Joined: Mon Nov 15, 2010 6:36 pm UTC

### Re: Problem With 0 At Start of Integer

Not an option. It's part of an assignment where I'm required to use an integer.

masher
Posts: 821
Joined: Tue Oct 23, 2007 11:07 pm UTC
Location: Melbourne, Australia

### Re: Problem With 0 At Start of Integer

How do you separate it currently?

I just did a quick test then, and I can write some code that will handle that. (assuming that MM/DD/YYYY is a string...)

cba
Posts: 72
Joined: Wed Oct 06, 2010 2:58 am UTC
Location: not here...
Contact:

### Re: Problem With 0 At Start of Integer

well if the input has to be given in a certain way (as in the input will always be given as mmddyyyy) as a number, then you could check the length and if its say 7 numbers long then u know that the first 0 it has been removed and then take the first digit as the month...

you would need to use strings at some point to split it anyway...
For the first time ever! flatland in a mind-boggling 3D!
import antigravity

_Axle_
Posts: 253
Joined: Fri Sep 24, 2010 7:33 pm UTC

### Re: Problem With 0 At Start of Integer

What language are you using and how are you currently parsing out a single int into 3 different ints?

Code: Select all

`// 03061990 sample inputconst int month = 1000000;const int day = 10000;int InputMonth = Input / month;  // Integer division should only return the whole number, 1-12 for what month, if input 01-12int currinput = Input - InputMonth * month; // Erase the high end month partint InputDay = currinput / day; // Same as month, checks to see what day num it iscurrinput = currinput - InputDay * day;// What is left is the year`

I thought of this in like 5-10 minutes, so most likely I missed something, but its a good step if your language supports integer only division
Yakk wrote:Computer Science is to Programming as Materials Physics is to Structural Engineering.

masher
Posts: 821
Joined: Tue Oct 23, 2007 11:07 pm UTC
Location: Melbourne, Australia

### Re: Problem With 0 At Start of Integer

cba wrote:you would need to use strings at some point to split it anyway...

You don't need it as a string to split it. You'll need the results of the "splitting" to make it "MM/DD/YYYY", but the actual method extracting the month, day and year can be done with mathematical operations.

.

_Axle_ wrote:What language are you using and how are you currently parsing out a single int into 3 different ints?

Code: Select all

`// 03061990 sample inputconst int month = 1000000;const int day = 10000;int InputMonth = Input / month;  // Integer division should only return the whole number, 1-12 for what month, if input 01-12int currinput = Input - InputMonth * month; // Erase the high end month partint InputDay = currinput / day; // Same as month, checks to see what day num it iscurrinput = currinput - InputDay * day;// What is left is the year`

I thought of this in like 5-10 minutes, so most likely I missed something, but its a good step if your language supports integer only division

This works, but I think that my method is a little more elegant. I'll post it after the OP reappears..

Blonk
Posts: 10
Joined: Mon Nov 15, 2010 6:36 pm UTC

### Re: Problem With 0 At Start of Integer

It's C++, sorry for not mentioning it.

And, yes, I was looking to do it by means of division/modulus.

masher
Posts: 821
Joined: Tue Oct 23, 2007 11:07 pm UTC
Location: Melbourne, Australia

### Re: Problem With 0 At Start of Integer

masher wrote:How do you separate it currently?

rflrob
Posts: 235
Joined: Wed Oct 31, 2007 6:45 pm UTC
Location: Berkeley, CA, USA, Terra, Sol
Contact:

### Re: Problem With 0 At Start of Integer

I haven't done C++ in years, and am not sure how you're reading in your number, but at least in Python, a number with a leading 0 is treated as an octal number. I'd also note that o40 == d32*. On the other hand, if it is in octal mode, it should barf when it hits an 8 or a 9.

What exactly are you getting as 32? The whole datestring, or just the month? Can you post any of your code so we can spot errors?

* Why do programmers confuse Christmas and Halloween? Because Oct 31 = Dec 25!
Ten is approximately infinity (It's very large)
Ten is approximately zero (It's very small)

Blonk
Posts: 10
Joined: Mon Nov 15, 2010 6:36 pm UTC

### Re: Problem With 0 At Start of Integer

rflrob wrote:I haven't done C++ in years, and am not sure how you're reading in your number, but at least in Python, a number with a leading 0 is treated as an octal number. I'd also note that o40 == d32*. On the other hand, if it is in octal mode, it should barf when it hits an 8 or a 9.

What exactly are you getting as 32? The whole datestring, or just the month? Can you post any of your code so we can spot errors?

* Why do programmers confuse Christmas and Halloween? Because Oct 31 = Dec 25!

Very interesting! This must be the case. There was a typo in the OP. It was supposed to read 04 at the beginning, not 03. Can you explain this a bit further?

rflrob
Posts: 235
Joined: Wed Oct 31, 2007 6:45 pm UTC
Location: Berkeley, CA, USA, Terra, Sol
Contact:

### Re: Problem With 0 At Start of Integer

In most languages, it's possible to input numbers in bases other than 10. This is often helpful for programmers, since computers actually store things as binary numbers, which are really easy to convert to octal (base 8; group bits into 3's) or hexadecimal (base 16; group bits into 4's). The problem with these other bases is that a number like 432 could be a valid octal or hexadecimal number, and would mean something totally different than four hundred thirty two. So, to be explicit, if you want something to be a hexadecimal number, you precede it with 0x (X sounds like Hex), and if you want it to be octal, you precede it with a zero (0 for 0ctal, I guess).

The more I think about it, though, the less I think this is what's actually going on. While 040 == 32, there's no guarantee that 04061776 would start with 32, so if you're pulling out the month using something similar to the above methods, you'd be unlikely to get 32 out; in fact, you know you'd get out something less than or equal to the input month. More importantly, however, 04061990 is *not* a valid octal number. If you're in base 8, the digit 9 does not exist, just as in base 10 we don't have a digit for eleven. I'm not sure what platform/compiler you're using, but my gcc throws an 'error: invalid digit "9" in octal constant', so unless you're somehow ignoring the rest of the number, at best you'd get weird errors.

Code listings would help us figure out whether this is actually the problem, or (as I suspect) a red herring.
Ten is approximately infinity (It's very large)
Ten is approximately zero (It's very small)

Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

### Re: Problem With 0 At Start of Integer

Some languages "helpfully" truncate integers when encountering an illegal digit instead of throwing errors.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

Blonk
Posts: 10
Joined: Mon Nov 15, 2010 6:36 pm UTC

### Re: Problem With 0 At Start of Integer

Here's a simplified version of what I'm talking about

Code: Select all

`int main() {   int patBirthday;   cout << "Enter birthday (MMDDYYYY): ";   cin >> patBirthday;        cout << patBirthday << endl << endl;}`

Enter '04091990' and it outputs 32. I have a feeling it's just stopping its octal "conversion" when it hits an invalid digit (like 9, as you said). I believe all I need to do is find a way to treat the integer as dec and not oct.

Also, I'm running Microsoft Visual C++ 5.0.

Edit:

Here are my results using Visual Studio 2010 with the following code.

Code: Select all

`#include "stdafx.h"#include "iostream"using namespace std;int main()  {   int patBirthday;   cout << "Enter birthday (MMDDYYYY): ";   cin >> patBirthday;   cout << patBirthday << endl << endl;   return 0;}`

When entering '04091990' the output was 4091990.

phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

### Re: Problem With 0 At Start of Integer

istream can be set to use either plan: either it can always work as if the number the user enters is in a specific base (usually decimal), or it can try to read it in the same way as a C++ literal (ie normally decimal, but switching to hex if it starts with 0x or octal if it starts with 0). The C++ standard says it should be in the first mode by default - unless you tell it otherwise, it'll try to read everything in decimal.

However, many ancient versions of C++ (which includes MSVC5, and I think also current versions of MSVC if you include <iostream.h> instead of the standard <iostream>) will default to the other way - unless you tell it otherwise, it will default to reading the number as a C++ literal, treating it as octal if there's a leading 0.

(1) Switch to a compiler that isn't 13 years old. MSVC10, a full 5 major versions upgraded from the compiler you're using, has an Express edition available for free.
(2) std::cin >> std::dec >> patBirthday;

Code: Select all

`enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}`
[he/him/his]

Blonk
Posts: 10
Joined: Mon Nov 15, 2010 6:36 pm UTC

### Re: Problem With 0 At Start of Integer

phlip wrote:istream can be set to use either plan: either it can always work as if the number the user enters is in a specific base (usually decimal), or it can try to read it in the same way as a C++ literal (ie normally decimal, but switching to hex if it starts with 0x or octal if it starts with 0). The C++ standard says it should be in the first mode by default - unless you tell it otherwise, it'll try to read everything in decimal.

However, many ancient versions of C++ (which includes MSVC5, and I think also current versions of MSVC if you include <iostream.h> instead of the standard <iostream>) will default to the other way - unless you tell it otherwise, it will default to reading the number as a C++ literal, treating it as octal if there's a leading 0.

(1) Switch to a compiler that isn't 13 years old. MSVC10, a full 5 major versions upgraded from the compiler you're using, has an Express edition available for free.
(2) std::cin >> std::dec >> patBirthday;

This was the trick here. The first method isn't an option since it's for a project I have to hand in where the person grading it is using the same outdated software I am to compile it.

Thank you so very much <3

phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

### Re: Problem With 0 At Start of Integer

Blonk wrote:the person grading it is using the same outdated software

Ah. In that case, you have my sympathies...

Code: Select all

`enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}`
[he/him/his]

thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

### Re: Problem With 0 At Start of Integer

I always prefer my conversions to be explicit. Try strtol(), or whatever the C++ equivalent would be.

Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery

MHD
Posts: 630
Joined: Fri Mar 20, 2009 8:21 pm UTC
Location: Denmark

### Re: Problem With 0 At Start of Integer

thoughtfully wrote:I always prefer my conversions to be explicit. Try strtol(), or whatever the C++ equivalent would be.

This is the canonically safe and explicit way.

Code: Select all

`std::string s;std::getline(std::cin, s);std::stringstream str(s);uint32_t i;str >> std::dec >> i;if (!str) {    std::cerr << "Error! Awrhagafbv." << std::endl}`
EvanED wrote:be aware that when most people say "regular expression" they really mean "something that is almost, but not quite, entirely unlike a regular expression"

Emu*
Posts: 689
Joined: Mon Apr 28, 2008 9:47 am UTC
Location: Cardiff, UK
Contact:

### Re: Problem With 0 At Start of Integer

Whoever declared the input would be an integer is a muppet. Sure it should be a valid integer, but it should always be a String unless you plan to do math with dates.

And if you do plan to do math with dates you want a Date object and a decent API.
Cosmologicon wrote:Emu* implemented a naive east-first strategy and ran it for an hour, producing results that rivaled many sophisticated strategies, visiting 614 cells. For this, Emu* is awarded Best Deterministic Algorithm!