Thread: Using Atoi and missing 0's

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    51

    Using Atoi and missing 0's

    I just figured out how to finally get the Atoi to work in my program but I can't seem to figure out how to make sure that it doesn't get rid of the 0's. For example i want to get 000012345 into an int BUT it just displays 12345 and deletes the 0. Anyway around this?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Shadowwoelf View Post
    I just figured out how to finally get the Atoi to work in my program but I can't seem to figure out how to make sure that it doesn't get rid of the 0's. For example i want to get 000012345 into an int BUT it just displays 12345 and deletes the 0. Anyway around this?
    The premise of the entire question is wrong. There is no such thing as "leading zeroes" on a value. A value is what it is. When it is written in a particular base it takes on a particular representation. The leading zeros are part of the representation, not the value.

    atoi() converts the representation to a value, which clearly is going to discard the irrelevant leading zeros. There is no way to get them back, and I don't see why you'd want to.

    If you want to print it back out exactly as it was entered, don't convert it to an integer in the first place.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    if you just want to display the number, your best option would be to print the original string to the screen rather than converting it to an int

    [edit]eh... 3 minutes... I'm slow...[/edit]

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It is possible to format numbers to be a specific width, right align them, and fill the space with zeros. But as others have said, why not just print the original string?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I had the same problem when trying to read a string from a file and attempt to match it to an integer, I decided to append a 1 to the start of the value, assuming that the value would not exceed the max size of an int. Not sure if that helps what you're trying, though.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Every number has an infinit number of leading 0's.
    All these numbers are exactly the same:
    3.14
    0003.14
    3.14000000
    00000000000000000000000000003.14000000000000000000 00000000

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > It is possible to format numbers to be a specific width, right align them, and fill the space with zeros.
    Like
    printf( "%06d", mynumber );

    In C++, use the setw() and setfill() manipulators.
    http://www.cplusplus.com/reference/i...tors/setw.html
    http://www.cplusplus.com/reference/i...s/setfill.html
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Well Sorry guys I had no Idea what I was thinking must have lost my brain when I wrote that heh. By the way BlackRoot thats the exact same problem I am dealing with right now but I can just assume that there wont be any 0's in the front of the password

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Password? Why on earth would you convert a password to an integer?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atoi convert problem
    By Ray Schmidt in forum C++ Programming
    Replies: 6
    Last Post: 02-07-2003, 11:05 PM