Thread: Loss of zero

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    Loss of zero

    Hi guys,

    I got a programming assignment and I am not allowed to use 'strings' to solve this specific problem I have encountered. In this program, the user inputs a fixed length number and the program collects the number via the scanf function. I needed to break the number into segments so I used division by 10^x (x varying) and also the modulus operation. The problem I have is that when I use the modulus operation and the number contains a zero, I lose that zero. I need the zero because the number is a barcode.
    For example:
    Barcode: 9484029480583
    If I wanted to extract the number 058 from that barcode (the last three numebrs minus the checksum) I would use the following operation:
    ((9484029480583)/(10))%(1000), but the problem is instead of the result being 058, I get just 58. I can overcome this problem by simply inputting a zero in front of the two numbers when using printf, but this would create extensively messy code because I would have a lot of 'if' statements (Im not allowed to use 'case' so don't go there). Is there a way that I can somehow retain that zero in front of the three number segment?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Say
    printf( "%03d", 58 );
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up flags for the printf format string (particularly flag 0 which means "left pad with zeros rather than spaces").
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thanks, but I don't think that helps me. Having a number between the integer input (%"number"d), is that different to having before or after? Sorry I have never put it in the middle. I tried it anyway, but it didn't work.
    Like I said, I could print the 0 in front by writing the code, but it would produce a massive headache of 'if' statements. I just get the feeling there is a simpler way of doing by altering the variable storing the number or something.

    I had a thought. What is it that is removing the leading zero. Is it the calculation and storing of the variable or is it the printing of the contents of the variable as an integer? Maybe we could solve it by attacking which ever one is causing it.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Ok thanks, yep I got it. Thanks to both of you. Yep I found the flags thanks.

  6. #6
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Like I said, I could print the 0 in front by writing the code, but it would produce a massive headache of 'if' statements. I just get the feeling there is a simpler way of doing by altering the variable storing the number or something.
    Yes. Using strings.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Freshquiz View Post
    What is it that is removing the leading zero. Is it the calculation and storing of the variable or is it the printing of the contents of the variable as an integer? Maybe we could solve it by attacking which ever one is causing it.
    Integers have no notion of characters. They just hold a 'quantity' if you will. 7, 07, and 007 don't actually constitute different whole numbers. They all represent seven whole items and thus an int holds this value of seven.
    How you format it is of course up to you. It may be seven dollars i.e. "$7.00" or seven cents ".07c" or seven O'Clock in the morning "07:00".
    Leading and or trailing zeros are just one of many things you can choose to do when formatting the value.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Economy crises, Job loss etc etc
    By ssharish2005 in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 01-27-2009, 03:08 PM
  2. conversion from double to float - loss of data
    By diyteam in forum C++ Programming
    Replies: 20
    Last Post: 03-04-2008, 02:59 AM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  4. Guaranteed weight loss
    By Glirk Dient in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-12-2004, 06:11 AM
  5. Configurations give different results
    By Hubas in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 11:43 AM