I am new to the C language and I have to create a program that registers users but when consulting them, the entered name does not appear, only its first two characters. For example, if the user is called Paco at the moment of consulting the registered users, only PA will be shown followed by a consecutive number such as PA001. Is there any way to achieve this?
#include<stdio.h>
#include<stdlib.h>
struct registro{
char nombre[100];
int opcion;
}personas[6];
int main(){
int choice;
int i;
do {
printf("Eliga una opcion:\n");
printf("1.Captura\n");
printf("2.Consulta\n");
printf("3.Salir");
scanf("%d", &choice);
if (choice==1){
printf("Captura\n");
for(i=0;i<5;i++){
fflush(stdin);
printf("Escribe tu nombre:\n",i+1);
gets(personas[i].nombre);
printf("\n");
}
for(i=0;i<5;i++){
printf("%i.Su nombre es:%s",i+1,personas[i].nombre);
printf("\n");
}
}
if (choice==2){
printf("Consulta\n");
for(i=0; i<5;i++){
printf("%i.Su nombre es:%s",i+1,personas[i].nombre);
printf("\n");
}
}
if (choice==3){
printf("Salir");
}
}while(choice !=3);
return 0;
}
Here is an example of how you can do it. Cheers
There are several shapes ... for tastes, colors :-)
We go through the chain and show its characters 1 by 1, up to the number that interests us.
printf( )
supports indicating how many characters we want to display:We can even pass an argument to
printf( )
, so that it is not a literal number :If we feel especially hardworking, and the string to display is not constant, we can place a terminator (the character
0
) by hand and restore it later: