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