Thread: strange integer problem

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    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!
    #include <Jesus.h>
    It will save your life

  2. #2
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    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. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    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"?
    #include <Jesus.h>
    It will save your life

  4. #4
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    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. #5
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    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--------------------------------------------------------------------------------
    Read the next post
    Last edited by CheesyMoo; 03-21-2003 at 11:05 PM.
    If you ever need a hug, just ask.

  6. #6
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    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. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    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.?
    #include <Jesus.h>
    It will save your life

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    20 in base eight is (2*8) + ( 0*1) so 16 decimal.
    15 in base eight is (1*8) + (5*1) so 13 decimal.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    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?
    #include <Jesus.h>
    It will save your life

  10. #10
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    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. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    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.
    Last edited by R.Stiltskin; 03-22-2003 at 11:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Strange problem with GETLINE
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 07-07-2008, 09:57 AM
  3. Strange problem
    By G4B3 in forum C Programming
    Replies: 6
    Last Post: 05-14-2008, 02:07 PM
  4. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  5. Strange problem
    By ~Kyo~ in forum Game Programming
    Replies: 0
    Last Post: 02-14-2006, 10:35 PM