C 将输出与每个标题对齐

C 将输出与每个标题对齐,c,string,C,String,此代码每次都要求输入名称、方向和数字,并将其保存在数组中。它会询问您是否要继续。0用于输入其他联系人,1用于打印列表。我尝试使用\t对齐标题下的每个项目。当我输入的字符串没有空格时,它会非常有效,但当我使用空格时,字符串不会对齐。有什么问题?我想不出来。提前谢谢 #include "stdafx.h" #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <string.h&

此代码每次都要求输入名称、方向和数字,并将其保存在数组中。它会询问您是否要继续。0用于输入其他联系人,1用于打印列表。我尝试使用\t对齐标题下的每个项目。当我输入的字符串没有空格时,它会非常有效,但当我使用空格时,字符串不会对齐。有什么问题?我想不出来。提前谢谢

#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]);

    }
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
void desplegar(int,char[][20],char[][20],char[][20]);
int main()
{
字符名称[20][20];
char direcciones[20][20];
字符编号[20][20];
int opcion=0;
int contactos=0;
系统(“主机名”);
做{
printf(“\nIngrese el nombre del contacto:”);
获取(nombres[contactos]);
printf(“\nIngrese la direccion del contacto:”);
获取(direcciones[contactos]);
printf(“\nIngrese el numero del contacto:”);
获取(numeros[contactos]);
contactos++;
printf(“\nDesea Ingrear otro contacto?(0/1):”;
scanf(“%d”和&opcion);
getchar();
}而(opcion!=1&&contactos<20);
desplegar(联系人、姓名、董事、数字);
printf(“\n”);
系统(“暂停”);
返回0;
}
无效数据表(内部数据表,字符名称[][20],字符目录[][20],字符编号[][20]){
printf(“名称\t\t建议\t\t建议\n”);
对于(inti=0;i
阅读
printf
格式说明符,不要使用选项卡。制表符是邪恶的,因为它们可以从零扩展到8(至少)个空格,破坏列格式

例如,如果第1列的宽度为20个字母,则文本使用“%20s”,数字使用“%20d”。您必须查看如何使用左对齐和说明符


注意:此方法仅对固定宽度字体有效。当使用可变宽度字体(如字符宽度、字符间距等)时,有更多的工作。

您的代码是清晰的C,为什么用C++来标记?左对齐是通过在数字之前加上减号来实现的。谢谢你的回答,我的程序正在运行。