1

I'm testing input formatting in C with a simple program to get familiar with C's commands, and I can't seem to figure out how to format this one output to be dynamic. So here's my problem: When you enter a name, the Salary, tax, and netpay don't adjust to the space making it look crummy. If I enter a looooong first name, it will move over on the same line and not appear under it's specified column that defines the number. Here's my code:

#include <stdio.h>

main()
{
    char name[20];
    char *nameptr = name;
    double netpay, tax, salary;

    printf("Enter Name: ");
    scanf("%s", nameptr);
    printf("\nEnter salary: ");
    scanf("%lf", &salary);

    tax = (salary * 0.25);
    netpay = salary - tax;

    printf("\n\nName\t\tSalary\t  Tax\t  Netpay\n");
    printf("-------------------------------------------\n");
    printf("%s", nameptr);

    printf("%15.2lf  - %.2lf  =  %.2lf\n\n",salary, tax, netpay);
}

I want the program to let the numbers stay where they are, and not adjust by the name length.

OR

If they have to adjust, can I use the length of the name to adjust the

printf("\n\nName\t\tSalary\t  Tax\t  Netpay\n");

and

printf("%15.2lf  - %.2lf  =  %.2lf\n\n",salary, tax, netpay);

together?

Is this possible in C?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Nack
  • 25
  • 7

1 Answers1

1

Use a variable field width * to align the output. "%*s". @Barmar
Use - to left justify. "%-*s"
Use the return value of printf() to make the "---".

What makes this approach good is that the Name_Width and Money_Width can be use to control the width of the column title and the width of the data.

  int Money_Width = 15;
  int Name_Width = strlen(nameptr);
  if (Name_Width < 4) Name_Width = 4; // "Name" width

  puts("");  // \n

  int width = printf("%-*s %*s %*s %*s\n", Name_Width, "Name", Money_Width,
      "Salary", Money_Width, "Tax", Money_Width, "Netpay");

  for (int i = 0; i < width; i++) {
    putchar('-');
  }
  puts("");  // \n

  printf("%-*s %*.2f %*.2f %*.2f\n", Name_Width, nameptr,
      Money_Width, salary, Money_Width, tax, Money_Width, netpay);

Sample output

Enter Name: asd

Enter salary: 123

Name          Salary             Tax          Netpay
-----------------------------------------------------
asd           123.00           30.75           92.25
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Ok, Thank you! Is there anyway, for the name not to be cut off after 5 characters? Like if I enter 8 characters will the numbers in the same line shift, or will they stay the same. – Nack Sep 16 '16 at 21:45
  • @Nack 1) `name[20]` must be big enough to handle the name (20 is small IMO, why not 100?) 2) My example name was 3 characters, what name did you try that was 8? If you want names with spaces, do not use `scanf("%s", nameptr);`. Recommend to use `fgets()` for all user input. – chux - Reinstate Monica Sep 16 '16 at 21:51
  • oh ok, Yea, my next test is with fgets(). I just wanted to test a simple program to work on formatting my output in C. I had tried Jennifer and it cut it at 5 characters – Nack Sep 16 '16 at 21:56
  • @Nack _it_ cut it at 5 characters --. Is _it_ the orignal code or this answer? – chux - Reinstate Monica Sep 16 '16 at 21:58
  • @Nack BTW: When working with money, consider uses whole amount of the smallest unit and/or rounding the numeric calculations. E.g. `tax = round((salary * 0.25)*100.0)/100.0;` – chux - Reinstate Monica Sep 16 '16 at 22:01
  • This answer. I had implemented your code, and I see your strategy. For some reason it cuts the name after 5 characters. – Nack Sep 16 '16 at 22:01
  • Nevermind, I figured it out. Had to do with scanf function. – Nack Sep 16 '16 at 22:03
  • Man, I really appreciate your help. I only come on this site as a last resort, after searching for hours and testing for hours for an answer. Thank you! – Nack Sep 16 '16 at 22:03