invalid conversion from 'char' to 'const char'

0

When I use the functions strcmp, strlen, strcat or strcpy I always get the same error, the program says the following:

Error: invalid conversion from 'char' to 'const char'.

Could someone help me?

I attach the code and a photo of a program that I am doing now and in which I get the error already commented.

voidbuscar_alumno(char dni) {

    int i;

    for (i = 0; i < miclase.num_alumnos; i++) {
        if(strcmp(dni, miclase.clase[i].DNI) == 0) {
            printf("\nEl primer apellido del alumno buscado es: %s", miclase.clase[i].primer_apellido);
            printf("\nEl segundo apellido del alumno buscado es: %s", miclase.clase[i].segundo_apellido);
            printf("\nEl nombre del alumno buscado es: %s", miclase.clase[i].nombre);
            printf("\nLa nota de teoria del alumno buscado es: %f", miclase.clase[i].nota_teoria);
            printf("\nLa nota de practica del alumno buscado es: %f", miclase.clase[i].nota_practica);
            printf("\nLa nota de final del alumno buscado es: %f", miclase.clase[i].nota_final);
            menu();
        }
        else {
            printf("\nNo se ha encontrado ningun alumno con el DNI introducido.");
            menu();
        }
    }
}
    
asked by manolo_sr 22.08.2017 в 10:55
source

1 answer

4
void buscar_alumno(char dni) {
//                 ^^^^ 

    for (i = 0; i < miclase.num_alumnos; i++) {
        if(strcmp(dni, miclase.clase[i].DNI) == 0) {
//                ^^^

Notice that, on the one hand, dni is char type, that is, you can only store a single character ... and on the other you are calling strcmp which is a function to work with character strings, that is, with variables of type char* , which are pointers.

The function should receive a pointer:

void buscar_alumno(char* dni) {

Although being a bit strict, if the variable should not be modified within the function, it would be correct if the variable were also constant:

void buscar_alumno(const char* dni) {

On the other hand, notice that your algorithm does not leave the loop once you have found a solution:

for (i = 0; i < miclase.num_alumnos; i++) {
    if(strcmp(dni, miclase.clase[i].DNI) == 0) {
        // ...
        menu();
    }

This will have an undesirable effect on your application and is that after doing a search the program will wait for you to leave the menu (presumably using the exit option) and then, instead If you leave the program, your code will continue looking for the user where you left off. Depending on how you design your application this can cause unexpected behaviors or behaviors.

The logical thing is to leave the loop and this can be achieved with break or by touching the for conditional:

for (i = 0; i < miclase.num_alumnos; i++) {
    if(strcmp(dni, miclase.clase[i].DNI) == 0) {
        // ...
        // menu();
        break;
    }

Notice that the call to menu() is commented ... is not necessary at all . Think that since menu() you have already made a call to this function ... when the execution leaves this function it will return to menu() without you having to worry about it.

Another error that I see is that your program will show messages saying that the user is not found even if it is not (and if it is not found, it will show a message for each record in the list).

The error message you should show once you have gone through the entire list, that is, it must be outside the loop. And how does the program know when to show the message if you are already out of the loop? You can choose to use additional variables:

int alumnoEncontrado = 0;

for (int i = 0; i < miclase.num_alumnos; i++) {
    if(strcmp(dni, miclase.clase[i].DNI) == 0) {
      alumnoEncontrado = 1;
      // ...
      break;
    }
}

if( alumnoEncontrado == 0 )
{
  printf("\nNo se ha encontrado ningun alumno con el DNI introducido.");
}
    
answered by 22.08.2017 / 11:15
source