954,479 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Print_Zero Function

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.

dallin
Newbie Poster
14 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

psuedo code:

if miles <100
  print 0 and miles
if feet < 1000
  print 0 and feet
if inches < 10
  print 0 and inches
prog-bman
Junior Poster
109 posts since Nov 2004
Reputation Points: 14
Solved Threads: 4
 

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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Test drive this to get another hint:
[php]// pad with zero using sprintf()

#include

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]

vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Test drive this to get another hint: [php]// pad with zero using sprintf()

#include

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 playCount the Undefined Behavior with that. ;)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You