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.

Recommended Answers

All 4 Replies

psuedo code:

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

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.

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

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. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.