Will not let me enter the data in an array structure

3

Excuse me, surely it is something without much difficulty for you but I am just starting in this language, my mistake is something logical that surely I can not understand since I feel that all the arithmetic sentences are made correctly. Keep in mind that the code lacks some technical aspects such as prototypes of functions, this happens because the code is not yet finalized.

The code is as follows:

#include <stdio.h>
#include <stdlib.h>
#define elementos 3

struct clientes{
    char nombres[30];
    char apellidos[20];
    char numero[10];
    char edad;
};

struct clientes clientes1[elementos];

int main()
{
    int opc=0;

    do{
        printf("MENU DE OPCIONES\n");
        printf("1- Altas\n");
        printf("2- Bajas\n");
        printf("3- Modificacion\n");
        printf("4- Salir\n");
        scanf("%d", &opc);

        while(opc<1 || opc>5){   //verifico si la opción ingresada esta en el rango
            printf("Por favor ingrese un numero del 1 al 4\n");
            scanf("%d", &opc);
        }

        switch(opc){
            case 1:
                system("cls");
                altas();
                system("pause");
                system("cls");
            break;
        }
    }while(opc!=4);

    return 0;
}

void limpiar(){
    system ("cls");
    printf("la pantalla esta limpia\n");
}

void altas(){
    int i;

    for(i=0; i<elementos; 
    i++) //aca se encuentra el error
    {
        printf("ingrese el nombre del cliente ");fflush(stdout);
        gets(clientes1[i].nombres);
        printf("ingrese el apellido de su cliente "); fflush(stdout);
        gets(clientes1[i].apellidos);
        printf("Ingrese el numero de telefono de su cliente ");fflush(stdout);
        gets(clientes1[i]. numero);

    }

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

        printf("su nombre es %s \n", clientes1[i].nombres);
        printf("su apellido es %s \n", clientes1[i].apellidos);
        printf("su numero de telefono %s \n", clientes1[i].numero);
    }
}

The output gives me the answer that when entering the option of discharges (1) it does not let me enter the first name but skips to the line of code to enter the last name, with the other iterations (two more in total) I have no problem, it only happens in the first iteration.

    
asked by Jay Alvarrez 15.03.2017 в 00:38
source

4 answers

1

You can use scanf to store the data directly, but it needs to be type array , same as you could use malloc .

This is the code with which you might notice your error:

#include <stdio.h>
#include <stdlib.h>
#define MAXIMO_CARACTERES 100

int main() {
    struct clientes {
        char nombres[MAXIMO_CARACTERES];
        char apellidos[MAXIMO_CARACTERES];
        int numero;
        int edad;
    } lista;

    printf("Escribe el nombre: ");
    scanf("%s", &lista.nombres);

    printf("Escribe el los apellidos: ");
    scanf("%s", &lista.apellidos);

    printf("Escribe un numero: ");
    scanf("%i", &lista.numero);

    printf("Escribe tu edad: ");
    scanf("%i", &lista.edad);

    printf("\nTe llamas: %s\nTus apellidos son: %s\nEscribiste el numero: %i\nEdad: %i\n", lista.nombres, lista.apellidos, lista.numero, lista.edad);
}

Another thing is that if age is a number it must be an integer type. I'll teach you the basic data types by comparing it with Lua :

Lua: i = 1 (entero i = 0)
C: int i = 1 (int(entero) i = 0)

Lua: palabra = "hola"
C: char *palabra = "hola";

Lua: flotante = 1.2
C: float flotante = 1.2;

Lua: function escribir(palabra) print(palabra) end (no retorna nada)
C: void escribir(char *palabra) { puts(palabra); };

Lua: function retornar(palabra) return palabra end (retorna la palabra en la función)
C: char *retornar(char *palabra) { return *palabra; };
    
answered by 15.03.2017 / 08:54
source
0

I can not answer for my raputation haha XD

But to compare the size of a list sizeof is used

char m[] = {1, 2, 3};
for (int i=0; i<sizeof(m); i++) {
      // código
}

If you want to compare a string in the array, use strlen including string.h in the header

    
answered by 15.03.2017 в 08:57
0

The problem you point out is a known scanf problem. What is recommended is clean the buffer with

while ((c = getchar()) != '\n' && c != EOF) { }

The getchar ()! = '\ n' loop works because when you call getchar (), the input stream is cleaned.

see: link

    
answered by 21.03.2017 в 00:55
0

I have tried the code on my mobile phone and the same thing happens to me. I have made a variation to see where the error is, executing the function altas directly. I think the error is in the fflush function that does not work properly. Try to put when you request the option entry the following:

scanf("%d%*c", &opc) ; 
    
answered by 21.03.2017 в 00:32