Comparison of a char data in a Struct

-2

How can I compare a data char from a Struct with 'H' or 'M' to know if the sex entered by keyboard is female or male ..?

    
asked by Jonathan jesus 18.10.2018 в 03:08
source

1 answer

3

Suppose you have the struct (for example) defined in the following way:

struct Registro{ 
   char sexo;
};

You can access the sex attribute using the operator ".".

Suppose I need to access the attribute of a single struct

char sexo = miregistro.sexo;

Suppose you have an array of N records of the form array_registers [N], you can enter the sex by doing: '

char sexo = array_registros[i].sexo;

And traverse it with a loop (for - while - do while) as it is more convenient for the developed case.

On how to compare them an easy way is the following:

char sexo = miregistro.sexo;
if(sexo == 'H')
{ //ejecuto alguna tarea
}
else if(sexo == 'M'){
//ejecuto algo
}

I hope you find it useful

    
answered by 18.10.2018 в 03:38