1. ## strange integer problem

hmm...I just finished an excercise were i'm using structs to get phone numbers from the user. But i noticed when you lead any one of the numbers (i mean area code, exchange, number) it ends up in a different number when it prints out. so i just tried something out.
Code:
```#include <iostream.h>

int main()
{
int x;

cin >> x;
cout << x;
return 0;
}```
so if i entered 015 it would print 13
when i enter 020 it prints 16
when i enter 010 it prints 8
why is this? thanks!

2. When you stick a 0 in front of a number, it will treat it as base 8.

base 8 - base 10 (decimal)
15 = 13
20 = 16
10 = 8
13 = 11

If you are merely getting their phone number, you can store it as a string rather than a integer.

3. Thanks for replying, but the way you explained it is unclear to me. Can you try putting a little more detail in there. I didn't really understand. and what do you mean when you said "and if you merely getting their phone number why not treat it as a string instead of a integer"?

4. In C or C++, I'm not sure about other languages, if you say:
Code:
`int x=010;`
you are changing the "base" of the number, to base 8. x would equal 8, in base 10. Normal numbers are in base 10, and we have digits ranging from 0-9. In binary, it's base 2, and you can have a 0 or a 1. For example, if I say 111, to find out what number that is in base 10, I would do the following:
Code:
`2^2+2^1+2^0=7`
Also, it sounds as if you are trying to store the entire phone number into an integer. You are never going to be "adding" these numbers, so you shouldn't treat them as numbers. They are merely text, so they should be made into strings.

5. Okay, there are different kinds of bases, base 2, base 3 etc. We use base 10 (decimal) where we have 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) that make up all other numbers (i.e. 43, 20034). Base 8 uses 8 different digits (0, 1, 2, 3, 4, 5, 6, 7) you also count differently.

I don't want to get into all about bases because I'm to lazy to talk about all that crap. Basically since you aren't going to do anything with the number (such as multiply it or divide it) then you don't need to have it as an integer.

So this would work fine:
Code:
```// bla bla bla all this top crap

int main()
{
char string[9];

cin.getline(string,9);
cout << string;

return 0;
}```
Also this way the user can enter 776-3456 with the '-' so it makes it less confusing.

Edit---------------------------------------------------------------------------------
Whoops you beat me to the post, his probably makes more sense then mine.
Edit2--------------------------------------------------------------------------------

6. Originally posted by CheesyMoo
So this would work fine:
Code:
```// bla bla bla all this top crap

int main()
{
char string[8];

cin.getline(string,8);
cout << string;

return 0;
}```
Also this way the user can enter 776-3456 with the '-' so it makes it less confusing. [/B]
Actually, you should probably declare it as string[9], you need room for the terminating \0 in a C string, which is what it looks like you are making.

7. Cool!

Thanks! i Understand the base part now. that pretty interesting that there's different bases in numbers, i never thought of it. So now my questions is :
if you put a 0 in front of a number it changes it to a base eight number. But How does 015 change to 13? 020 change to 16 etc.?

8. 20 in base eight is (2*8) + ( 0*1) so 16 decimal.
15 in base eight is (1*8) + (5*1) so 13 decimal.

9. Thanks Stoned_Coder. Man, it seems like you are on this site always

It all makes sense now. So how many different types of bases are there? and since you add a 0 in front of a number to change it to base 8, is different ways to change numbers to different bases?

10. When declaring an int, you could make it hexadecimal, base 16, by doing declaring it as '0xf0'. Which would be 16 in base 10. Hexadecimal numbers use the digits 0-9, and then the letters a-f as digits. I'm not sure if there are any other ways to change the base of a number when declaring it.

11. Blanket, HaLCyOn:

That automatic conversion of your input to base 8 seems to be a peculiarity of OLD Visual C++ i/o functions. I can't find any other compiler that would change the base of an integer INPUT merely because you precede it with a 0. (However, they will all handle the initial definition of an integer that way.)

Just to be sure, I tried your code using Borland CPP Builder 5.5, Dev-C++, and Sun (Unix) CC compilers. With all of them, if you input 013, the output is 13, 020 gives 20, etc.

And even with MSVC 6.0, if you use the standard C++ headers, your problem goes away. Instead of

Code:
`#include <iostream.h>`
you should use

Code:
```#include <iostream>
using namespace std;```
and your inputs will all be treated as decimal numbers.

THEN, if you want your integer input to be interpreted as octal, precede it with the appropriate manipulator, such as:

Code:
`cin >> oct;`
and all subsequent integer inputs will be interpreted as octals, with or without a leading 0 (until you override it with another manipulator).

But I agree with HaLCyOn and CheesyMoo's comments that string would be a more appropriate data type for phone numbers, since their lengths and formats can vary, and you won't be doing arithmetic with them anyway.