-1

I was trying to make a program in C that basically asks the user some information about the people they live with. The code is in Spanish, but I will show you the problem.

    /*Miembros de la familia*/
#include<stdio.h>
#include<stdbool.h>
#define TRUE 1
#define FALSE 0

int main(){
    
    int personas,i,varones=0,hembras=0,opcion;
    bool mayoredad=false;
    

    printf("Indique cuantas personas viven en su casa:\n");
    scanf("%i", &personas);

    
    struct nombre{
        char primer[30];
        char segundo[30];
        char apellido[30];
    }minombre;
    
    struct fecha{
        int dia;
        int mes;
        int anio;
    }nacimiento, actual;
    
    printf("\nIngrese la fecha actual:\n");
    scanf("%i %i %i", &actual.dia, &actual.mes, &actual.anio);
    
    
    struct familia{
        struct nombre minombre;
        char cedula[10];
        struct fecha nacimiento;
        char genero;
        int edad;
    }familia[personas];
    
    
    for(i=0;i<personas;i++){
        printf("\nIndique su primer nombre, segundo nombre y apellido:\n");
        scanf("%s %s %s", &familia[i].minombre.primer, &familia[i].minombre.segundo, &familia[i].minombre.apellido);
        printf("\nPor favor escriba su numero de cedula:\n");
        scanf("%s", &familia[i].cedula);
        do{
        printf("\nIngrese la fecha de su nacimiento: (DD)(MM)(AAAA):\n");
        scanf("%i %i %i", &familia[i].nacimiento.dia, &familia[i].nacimiento.mes, &familia[i].nacimiento.anio);
        if(familia[i].nacimiento.anio>actual.anio){
            printf("Dato invalido, por favor intente nuevamente.");
        }
        }while(nacimiento.anio>actual.anio);
        familia[i].edad=actual.anio-familia[i].nacimiento.anio;
            if(familia[i].nacimiento.mes>=actual.mes && familia[i].nacimiento.dia>actual.dia){
                familia[i].edad--;
            }
            if(familia[i].edad>=18){
                mayoredad=true;
            }
        do{
            printf("Indique su genero: (f) o (m):");
            scanf(" %c", &familia[i].genero);
            if(familia[i].genero=='f'){
                hembras++;
            }else if(familia[i].genero=='m'){
                varones++;
            }
        }while(familia[i].genero!='f' && familia[i].genero!='m');       
    }
    
    do{
    printf("Registro concluido. Desea ver las estadisticas? 1(si) 2(no)");
    scanf("%i", &opcion);
    if(opcion!=1 && opcion!=2){
        printf("DATO INVALIDO, INTENTE NUEVAMENTE");
    }else if(opcion==1){
        for(i=0;i<personas;i++){
            printf("Nombre: %s %s %s\n", familia[i].minombre.primer, familia[i].minombre.segundo, familia[i].minombre.apellido);
            printf("Cedula:%s\n", familia[i].cedula);
            printf("Edad:%i\n", familia[i].edad);
            printf("Mayor de edad:\n");
            switch(mayoredad){
                case true:printf("Si");break;
                case false:printf("No");
            }
        }
        printf("Cantidad de personas en el hogar: %i\n", personas);
        printf("Varones: %i    Hembras: %i\n", varones, hembras);
    }
    }while(opcion>=2 && opcion<0);
        
    printf("Presione una tecla para salir.");
    getchar();
    return 0;
}

In the last do-while loop that requests the person's gender (familia[i].genero) It is supposed to ask just once, but in the last iteration of the for loop, it displays the same question four times:enter image description here How can I fix this?

| improve this question | |
New contributor
Leonardo Contreras is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
0

The format specifier %i will treat input whose suffix is 0 (and not 0x nor 0X) as octal number.

Therefore, 09 in your input is invalid as octal number and it will leave 9 in the stream.

This 9 is read to familia[i].nacimiento.anio by the next specifier.

Then, following scanf(" %c", &familia[i].genero); will read 2003 from the input and this will lead to the 4 extra asking.

To fix this, use %d specifier to read integers. %d spcifier will treat input as decimal number regardless of the suffix.

| improve this answer | |

Your Answer

Leonardo Contreras is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.