0

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.

4
Contributors
4
Replies
6
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0

psuedo code:

if miles <100
  print 0 and miles
if feet < 1000
  print 0 and feet
if inches < 10
  print 0 and inches
0

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.

0

Test drive this to get another hint:

// 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;
}
0

Test drive this to get another hint:

// 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;
}

Let's play Count the Undefined Behavior with that. ;)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.