944,098 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3311
  • C RSS
Feb 2nd, 2005
0

Print_Zero Function

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
dallin is offline Offline
14 posts
since Jan 2005
Feb 2nd, 2005
0

Re: Print_Zero Function

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
Reputation Points: 14
Solved Threads: 4
Junior Poster
prog-bman is offline Offline
108 posts
since Nov 2004
Feb 2nd, 2005
0

Re: Print_Zero Function

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 3rd, 2005
0

Re: Print_Zero Function

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]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Feb 3rd, 2005
0

Re: Print_Zero Function

Quote 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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Can't Display These Topics By Sort?!!
Next Thread in C Forum Timeline: Help for graphics in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC