Table C

Solved
MissRobet Posted messages 5 Status Membre -  
[Dal] Posted messages 6373 Status Contributeur -
Salut, je veux obtenir un affichage bien ordonné comme un tableau de données. Cependant, voici ce que mon programme affiche, et je ne comprends pas pourquoi cet affichage retourne à la ligne pour afficher les zéros et quelques autres variables, alors que j'ai inséré le \n seulement à la fin de l'affichage de chaque ligne ! Pouvez-vous m'aider à trouver une solution, s'il vous plaît ?

printf("ID reservation\t\tNumero Passeport\t\tNom du voyageur\t\tDate de Naissance\t\tDepartArrivee\t\tVille de destination\t\tPays de destination\t\tVoyage en groupe: choisir 0 ou 1\t\tNumero du Groupe\n");

for (i=0; i<=Nbr_de_Voyages; i++)
{

printf("%i\t\t", Reservation[i]);
printf("%s\t\t", NumPasseport[i]);
printf("%s\t\t", NomVoyageur[i]);
printf("%s\t\t", DateNaisVgr[i]);
printf("%s\t\t", DateDpt[i]);
printf("%s\t\t", DateArv[i]);
printf("%s\t\t", VilleDest[i]);
printf("%s\t\t", PaysDest[i]);
printf("%i\t\t", Grp_V_F[i]);
printf("%i\t\t", NumGrp[i]);

printf("\n");

Il n'y a que la ligne en rouge qui s'affiche sous la forme que j'avais attendue.

}

1 answer

[Dal] Posted messages 6373 Status Contributor 1 106
 
Instead of using tabs to align the content of your columns, you could use printf with a size specifier.

For example,
printf("%7s ",NumPasseport[i]);
if you know that your passport numbers will not exceed 7 characters. You should also use shorter column headers that are based on the width of the data in the column; otherwise, it won’t look great, and align them using the same method.

Dal
2
[Dal] Posted messages 6373 Status Contributor 1 106
 
It is also possible to specify the size parameter value in printf, using the following notation:
int lmax = 7; printf("%*s ", lmax, NumPasseport[i]);

With this method, and by ensuring that your program analyzes the data of a column before displaying its content, to determine the maximum width needed, you can create a program that automatically adjusts the width instead of hardcoding it.
0
MissRobet Posted messages 5 Status Member
 
I tried using both: %7s and \t but the data is not written under their headings (the first line)



whereas when I only used %.7s ... the result was as follows

0
[Dal] Posted messages 6373 Status Contributor 1 106
 
7 is an example of the column width suitable for the passport number column given the data you enter, with an extra space as in my example (or a tab if you prefer, but one or more spaces seem better to me, as you will then have the same spacing between each column) and provided that the column title itself does not exceed 7 characters with a space (or tab), so a title like "N° Pass" for example and not "Numero Passeport" which is too long.

I give you indications in the form of examples as repeated several times. It is up to you to do the exercise, not me :-)

The size for the traveler's name, in your case, use up to 20 characters. Of course, if you apply a %7s formatting you will not get the expected result...
0