Print_Zero Function

Reply

Join Date: Jan 2005
Posts: 14
Reputation: dallin is an unknown quantity at this point 
Solved Threads: 0
dallin dallin is offline Offline
Newbie Poster

Print_Zero Function

 
0
  #1
Feb 2nd, 2005
I have a Distance Structure that includes Miles, Feet, and Inches. The user inputs these amounts 3 digits for miles, 4 digits for feet and 2 digits for inches. Not all of the time the user will input the full number of digits, when the user doesn't input the full number of digits I need to back fill the numbers with zero's for display. Like 97 for miles I would display is 097. The zero would be added to the user input by a call to the Print_zero function. I need some ideas on how to incorporate the back fill zero into a function. Thanks.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 108
Reputation: prog-bman is an unknown quantity at this point 
Solved Threads: 3
prog-bman prog-bman is offline Offline
Junior Poster

Re: Print_Zero Function

 
0
  #2
Feb 2nd, 2005
psuedo code:
  1. if miles <100
  2. print 0 and miles
  3. if feet < 1000
  4. print 0 and feet
  5. if inches < 10
  6. print 0 and inches
Join me on IRC:
Server: irc.daniweb.com
Channel: #C++

Chat Via:
http://daniweb.com/chat/minichat.php
or
Your favorite IRC client.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Print_Zero Function

 
0
  #3
Feb 2nd, 2005
Study up on printf(), particularly the width format specifier. Apply this to sprintf() within your function. Most help files that come with the compilers should give you all the details.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,959
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 918
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Print_Zero Function

 
0
  #4
Feb 3rd, 2005
Test drive this to get another hint:
[php]// pad with zero using sprintf()

#include <stdio.h>

int main()
{
int mile; //, foot, inch;
char *pad;

printf("Enter miles: ");
scanf("%d", &mile);
fflush(stdin); // flush input stream

// pad with max 3 zeros (this is a zero not an ohh)
sprintf(pad,"%03d",num);
printf("Zero padded = %s miles\n",pad);

getchar(); // wait
return 0;
}
[/php]
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Print_Zero Function

 
0
  #5
Feb 3rd, 2005
Originally Posted by vegaseat
Test drive this to get another hint:
[php]// pad with zero using sprintf()

#include <stdio.h>

int main()
{
int mile; //, foot, inch;
char *pad;

printf("Enter miles: ");
scanf("%d", &mile);
fflush(stdin); // flush input stream

// pad with max 3 zeros (this is a zero not an ohh)
sprintf(pad,"%03d",num);
printf("Zero padded = %s miles\n",pad);

getchar(); // wait
return 0;
}
[/php]
Let's play Count the Undefined Behavior with that.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC