Getting integers to hold zeros

This is a discussion on Getting integers to hold zeros within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm sure this is a stupidly easy question, but it must be asked: How would one go about ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    Getting integers to hold zeros

    Hey all, I'm sure this is a stupidly easy question, but it must be asked:

    How would one go about getting integers to hold zeros before any other number?
    Like, to get an integer to hold the value 001 and not just 1.

    I know it's probably been answered before, but it's such a simple question that I think it'd
    be more work searching for the answer than asking the question again.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,662
    That cannot be done. What can be done, however, is to format the number such that it is padded with the required number of zeros in an output stream or a stringstream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Wolves View Post
    Hey all, I'm sure this is a stupidly easy question, but it must be asked:

    How would one go about getting integers to hold zeros before any other number?
    Like, to get an integer to hold the value 001 and not just 1.

    I know it's probably been answered before, but it's such a simple question that I think it'd
    be more work searching for the answer than asking the question again.
    The real question that you are attempting to ask regards (possibly) the underlying storage of a value and the representation of a value in text with regard to a certain base.

    One is one is one. Looking at it differently never changes the value, only the representation. (Although a larger number would be more helpful in this awful analogy.)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    I might have an idea for what you want. With some creativity and a bit mask or two it will work.

    First, pick the number that you want, say...

    int value = 255;

    then, say you want three 0's before 255, to be 000255, what we have to do is, change the value to store 1000255.

    then use a bitmask to only retrieve the 000255 portion, you could programmatically search for the first 1, and through some logic count the 0's, then format the output correctly.

    Does that sound close to what you want? Oh and laserlight, anything can be done. ;-)
    Last edited by simpleid; 05-26-2007 at 06:32 PM.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by simpleid View Post
    then, say you want three 0's before 255, to be 000255, what we have to do is, change the value to store 1000255.
    Chalk up two that need to understand value vs text representation.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    dave, i'm not sure what you're trying to say but i think you're making a sarcastic remark at me so allow me to point out that the 'representation' doesn't matter, the effect is created which is what was asked for. who cares if he thinks 001 + 1 = 2 is different from 1+1=2, if he wants his output to be 00x then so be it, it's possible.

    I do understand the difference, but that doesn't mean I can't create any hypothetical effect I please regardless.

    Or maybe you're vision is too acute to see the greater good here, to let individuals do as they please. how ever meaningless something is -to you- means nothing, if someone has a purpose then to help them achieve that is respectful.

    Chalk up one simpleton that doesn't 'get it', seems some people around here prefer to argue their way out of providing creative solutions in a clouded argument citing 'irrationality.'
    Last edited by simpleid; 05-26-2007 at 06:47 PM.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by simpleid View Post
    dave, i'm not sure what you're trying to say but i think you're making a sarcastic remark at me so allow me to point out that the 'representation' doesn't matter
    Well, values don't have "leading zeros". Or any value does. So 255 or 1000255 both do or both don't. As a value.

    As a text representation, there is a difference. But you don't bit mask a text representation, so your solution seems ambiguous.

    And then there is the idea of a bit mask for 1000000. It would seem more suited to the % operator. But again, that would be value-land.

    But if it were the case that you added a million and then "modded" by a million, then you'd be left with a value of 255. Which of course suffers the initial problem; recurse indefinitely.

    So in regard to the OP's question: it's a non-question. Use printf specifiers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    say you create a game which keeps track of a score and you want it to have a limit. you want to display the score as 00001, kill another guy, 00002, maybe 99999 is the limit.

    and lets say for the hell of it, you want to track the zeros in a single int. if for nothing more than the intellectual curiosity to see it done.

    well i'll come up with the code later tonight after i close up a project, i'll post it soon. my motivation is nothing more than our discourse.

    >:-)
    Last edited by simpleid; 05-26-2007 at 08:46 PM.

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Dandy. Along the way, make mental notes of "representation" and "value".

    Keep in mind that 01 (octal 1), 0x1 (hexadecimal 1), 1 (decimal 1) are all the same value; they are different representations.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    i don't know what point you're making. if he's asking for something and you're just stating facts which don't stop an individual from giving a solution, then what are you saying?

    You might want to consider that there's a difference in how something is shown and what it means. Aesthetics might be an angle you're not considering?

    It sounds like what you're doing is making a point which doesn't contribute to the issue he's having. so were you really just being sarcastic?

    if i ask someone to tell me how the toaster works, and someone tells me about the way butter looks on the bread while in the toaster, then that doesn't really say anything about how to continue does it?

    what's your intention?
    Last edited by simpleid; 05-26-2007 at 09:27 PM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Wolves View Post
    Like, to get an integer to hold the value 001 and not just 1.
    The rest is smoke and mirrors.

    Quote Originally Posted by simpleid View Post
    i don't know what point you're making. if he's asking for something and you're just stating facts which don't stop an individual from giving a solution, then what are you saying?
    You've missed the previously presented solutions?
    Quote Originally Posted by laserlight View Post
    That cannot be done. What can be done, however, is to format the number such that it is padded with the required number of zeros in an output stream or a stringstream.
    Quote Originally Posted by Dave_Sinkula View Post
    Use printf specifiers.
    what's your intention?
    What is yours? Disinformation?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    Actually, what i've planned out and am working on so far seems to be working.

    I'm using a single bit as an indicator of where leading zeros begin then find the next bit where the value will be extracted. the program keeps track of the limit the single integer can take, designated by the number of places that can be used. this seems to be providing some extra functionality, and it's all in a single number.

    doing this with setValue (int va, int number_places), then retrieve with getValue (int val)

    1. if num_digits = 5 in setValue then there will be 5 places for values. ( 00000 )
    2. if 200 is passed to setValue then we will have 00200 returned.
    3. showValue will return 00200 without knowing num_digits.

    is that the "disinformation" you're referring to?

    see this way the leading zeros are not always the same, it's actually based on how many values there is allowed.
    Last edited by simpleid; 05-26-2007 at 09:42 PM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    All righty, you win. You're reinventing printf. Manipulate a representation and not a value; just what I said. I bow before you.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    well, i didn't really realize printf did it that way exactly, i just assumed there wasn't an obvious answer since he asked. and i tend to think 'do from scratch' before i think 'does it exist.' /faulty. i know i was mistaken. this was enlightening anyway.
    Last edited by simpleid; 05-26-2007 at 10:06 PM.

  15. #15
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    BLAH BLAH BLAH:

    printf("%03i\n", 1);

    As laserlight already put it, storing a value in memory, or representing it on your screen are two different things. Just imagine using the number one, i.e., 1, in some arbitrary mathematical operation. Does it matter whether you _write_ it as 001, 0000001, or just plain and simple 1? Of course not.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 01:24 PM
  3. Integers into array.
    By livestrng in forum C Programming
    Replies: 10
    Last Post: 10-29-2008, 11:35 PM
  4. Reading integers from a File into an Array
    By bobby19 in forum C Programming
    Replies: 13
    Last Post: 10-09-2006, 01:36 AM
  5. Replies: 6
    Last Post: 08-04-2003, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21