0

This code asks for a name, a direction, and a number each time and saves it in an array. It asks you if you want to continue. 0 is for entering another contact and 1 is for printing the list. I tried to align each item under its title using \t it works perfectly when the strings I enter do not have spaces, but when I use spaces the strings dont align. What is the problem? I cant figure it out. Thanks in advance.

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

void desplegar(int, char[][20], char[][20],char[][20]);

int main()
{
    char nombres[20][20];
    char direcciones[20][20];
    char numeros[20][20];
    int opcion = 0;
    int contactos = 0;

    system("hostname");

    do {

        printf("\nIngrese el nombre del contacto: ");
        gets(nombres[contactos]);

        printf("\nIngrese la direccion del contacto: ");
        gets(direcciones[contactos]);

        printf("\nIngrese el numero del contacto: ");
        gets(numeros[contactos]);

        contactos++;

        printf("\nDesea ingresar otro contacto? (0/1): ");
        scanf("%d",&opcion);
        getchar();
    }while(opcion != 1 && contactos < 20);

    desplegar(contactos,nombres,direcciones,numeros);


    printf("\n");
    system("pause");
    return 0;
}

void desplegar(int cantidad,char nombres[][20],char direcciones[][20],char numeros[][20]){
printf("Nombres\t\tDirecciones\t\tTelefono\n");

    for (int i = 0; i < cantidad; i++){ 

        printf("%s\t\t%s\t\t%s\n",nombres[i],direcciones[i],numeros[i]);

    }
}
Lord Pepito
  • 291
  • 3
  • 15

1 Answers1

0

Read up on the printf format specifiers and do not use tabs. Tabs are evil because they can be expanded from zero to 8 (at least) spaces and wreck your column format.

Example, if column 1 is 20 letters wide, use "%20s" for text or "%20d" for numbers. You'll have to look up how to use left justification with the specifiers.

Note: This method is only valid for fixed-width fonts. There is more work when using variable width fonts (such as character widths, spacing between characters, etc.)

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Left justification is achieved by placing a minus before the number. Thank you for your answer, Ive got my program working now. – Lord Pepito Oct 10 '13 at 00:02