Mouttie Dje Mouttie Dje - 1 month ago 15
C Question

printing results in a formated form

I want to format the result so it would be adjusted

output

Here is what I wrote so far:

void affichage(int note[][50],char nom[][50],float moy[], float moy_e[], char module[][50],int nb_etudiant, int nb_module){
int i,j;
printf("\n\t\taffichage\n\n");
printf("\t");
strcpy(module[nb_module],"Moy G");
strcpy(nom[nb_etudiant],"Moy C");
for (i=0;i<nb_module;i++){
printf("%5s ",module[i]);
}
printf(" %5s",module[nb_module]);
printf("\n");
for(i=0;i<nb_etudiant;i++){
printf("%-5s ",nom[i]);
for (j=0;j<nb_module;j++){
printf("%-7d ", note[i][j]);
}
printf(" %-7.1f\n",moy_e[i]);
}
printf("\n\n");
printf("%5s ",nom[nb_etudiant]);

for (i=0;i<nb_module;i++){
printf("%-7.1f ",moy[i]);
}
}

Answer

As a suggestion, printf returns the length of the string formatted.
Since you want to display the student's data, if we suppose you have a table
with 2 columns:


X chars Y chars -------|------ -------|--------- | | | ---------------------------------------- nom | prénom | note ---------------------------------------- Jane | Mari | 15 ---------------------------------------- Robert | Daniel | 16 ----------------------------------------- X : number of white spaces - 2 Y : number of white space - 2 it can be any value, but make sure it will be greater than most of
the lengths of the property displayed in that column

For Each student, for the first cell (nom) you should first print his name, then you get the length of the printed name PN (printf's returned value), and print ( X - PN) whitespace, then you add the pipe character |. You do the same for the next property which is prenom, and at last you print \n for a new line.

#include <string.h>
#include <stdio.h>

typedef struct
{
    char firstName[25];
    char lastName[25];
    float grade1;
    float grade2;
}Student;

Student createStudent(char* firstName, char* lastName, float grade1, float grade2) {
    Student student;

    strcpy(student.firstName, firstName);
    strcpy(student.lastName, lastName);
    student.grade1 = grade1;
    student.grade2 = grade2;

    return student;
}

/* each column has a width equal to 20 characters 2 for borders and 18 of whitespaces or characters */
void printBorder() {
    int i = 0;
    int len = 0;

    for(i =0 ; i<80; ++i) printf("-"); // top border
    printf("\n"); // breakline

    printf("|"); // left border of the left cell
    len = printf("First Name"); // len is equal to 10

    for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
    printf("|"); // print the right border of the cell
    len = printf("Last Name"); // len is equal to 9

    for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
    printf("|");
    len = printf("Grade 1"); // len is equal to 7

    for(i=0; i<18 - len; ++i) printf(" ");
    printf("|");
    printf("Grade 2\n");

    for(i =0 ; i<80; ++i) printf("-"); // bottom border of header
    printf("\n"); // breakline
}

void printBody(Student* students, int nbrOfStudents) {
    int j = 0; // to iterate over students
    int len = 0;
    int i =0; // for drawing
    for(j = 0; j< nbrOfStudents; ++j) {
        printf("|"); // left border of the left cell
        len = printf("%s", students[j].firstName);

        for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
        printf("|"); // print the right border of the cell
        len = printf("%s", students[j].lastName);

        for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
        printf("|");
        len = printf("%.2f", students[j].grade1);

        for(i=0; i<18 - len; ++i) printf(" "); // printing whitespace
        printf("|");
        len = printf("%.2f\n", students[j].grade2);

    }

    for(i =0 ; i<80; ++i) printf("-"); // bottom border of header
    printf("\n"); // breakline
}


int main() {
    Student students[3] = {
        createStudent("Jane", "Roberson", 99, 75),
        createStudent("Amelia", "Coeur-de-lait", 85, 89),
        createStudent("Anna", "Stone", 65, 30)
    };

    printBorder();
    printBody(students, 3);

    return 0;
}