1

I have an array of structures, where each structure contains a first name and a last name, along with other information. I'm trying to print the array in tabular form, something like this

+---------------------------+--------+--------+
| Student Name              | Test 1 | Test 2 |
+---------------------------+--------+--------+
| Pousseur, Henri           |   95   |   92   |
| Boyer, Charles            |   90   |   97   |
+---------------------------+--------+--------+

However, my output looks like this

+---------------------------+--------+--------+
| Student Name              | Test 1 | Test 2 |
+---------------------------+--------+--------+
| Pousseur, Henri                  |   95   |   92   |
| Boyer, Charles                     |   90   |   97   |
+---------------------------+--------+--------+

My question is, how do I concatenate the last name to the first name (with a comma in between), and then print the whole thing as a fixed width string? So that my table lines up correctly.

Here's my code:

#include <stdio.h>
#define NAME_LEN 25

typedef struct
{
    char fname[NAME_LEN+1];
    char lname[NAME_LEN+1];
    int score1;
    int score2;
} STUREC;

void printRecords(STUREC records[], int count)
{
    printf("+---------------------------+--------+--------+\n");
    printf("| Student Name              | Test 1 | Test 2 |\n");
    printf("+---------------------------+--------+--------+\n");

    for (int i = 0; i < count; i++)
        printf ("| %-s, %-26s| %4d   | %4d   |\n", records[i].lname, records[i].fname, records[i].score1, records[i].score2 );

    printf("+---------------------------+--------+--------+\n");
}

int main(void)
{
    STUREC records[] = {
        { "Henri"  , "Pousseur", 95, 92 },
        { "Charles", "Boyer"   , 90, 97 }
    };

    printRecords(records, 2);
}
11
  • Does the code you have posted have any bearing on the question? Commented Apr 20, 2016 at 1:43
  • 4
    use sprintf or snprintf. Commented Apr 20, 2016 at 1:43
  • 1
    Please try to post a minimal example - be specific about the issue you are having and what you have tried, and post the LEAST amount of code needed to reproduce the issue. Commented Apr 20, 2016 at 1:44
  • well i just wanted to show the code... my issue is i can't get the records[count].fname to concatenate onto the end of records[count].lname... i tried using the code i posted in the in the begining, i tried using strcat(s1, s2); Commented Apr 20, 2016 at 1:47
  • when i try strcat(records[count].fname, records[count].lname); it compiles, but changes absolutely nothing. Commented Apr 20, 2016 at 1:50

2 Answers 2

1

Note that printf returns the number of characters printed. So if you want to print the name within a fixed width, you can print the name, and then output spaces as needed to fill the width.

#define WIDTH 26

int count = printf( "%s, %s", records[i].lname, records[i].fname );
if ( count < WIDTH )
    printf( "%*s", WIDTH-count, "" );

In the format string "%*s", the * tells printf that the width of the string will be passed as an argument. The WIDTH-count argument is the number of spaces that need to be printed, and "" is just an empty string. So for example, if count is 16, then WIDTH-count is 10, so the printf is equivalent to

printf( "%10s", "" ); 

which will print 10 spaces.

Sign up to request clarification or add additional context in comments.

22 Comments

@seanncurtis This code replaces the code that prints the name, where you have printf ("| %-s, %-26s ....
You need to take a step back, and learn how printf works, instead of going off on a tangent with concatenation. For example, printf("hello "); printf("world\n"); does the same thing as printf("hello world\n");
You forgot to remove the records[i].lname.
Ok, I don't really understand that, but if you got it working, great!
and there it is... perfect. although i wish i understood how to operate the strcat function, this is a nice trick i've learned. thank you very much!
|
1

This is just an adpatation of @user3386109 answer.

Add

#define WIDTH 27 // You get a max width of 26

somewhere in the beginning of your program and a string

char fullname[WIDTH];

somewhere in the beginning of the main.

Then make changes like below :

printf("\n+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");
    printf("| Student Name              |   ID   | Test 1 | Test 2 | Proj 1 | Proj 2 | Proj 3 | Average | Grade |\n");
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    for (i = 0; i < count; i++)
    {
       int lname_len=strlen(records[i].lname);
       int fname_len=strlen(records[i].fname);
        snprintf(fullname,WIDTH,"%s,%s%*s", records[i].lname,records[i].fname,(WIDTH>(lname_len+fname_len)?(WIDTH-(lname_len+fname_len)):0),"");

        /* The above step concatenates - since you're very interested in it -
         * the sirname and the first name into a string fullname making sure
         * that the string is padded to 26 characters utmost.
         */

        printf ("| %s | %-7.6d| %4d   | %4d   | %4d   | %4d   | %4d   | %6.2f  |  %-2s   |\n", fullname, records[i].id, records[i].score1,
                 records[i].score2, records[i].score3, records[i].score4, records[i].score5,
                  records[i].ave, records[i].grade);
    }
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    printf("\nHave A Terrible Day!\n\n");

Note the ternary expression :

(WIDTH>(lname_len+fname_len)?(WIDTH-(lname_len+fname_len)):0)

in snprintf. This will be evaluated to unused width, if any which we pad with emptry strings "" or null characters.

If you didn't understand it please have a look here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.