Error searching for value in a matrix

-1

I'm doing a search for a string in an array, but it flags me in the IF. The error arises when compiling: "[Error] invalid conversion from 'char' to 'const char *' [-fpermissive]

void listado(char pers[100][9], int& lim){

    char busqD[40];
    int k;

    printf("\nCONSULTAS\n\nIngresar el nombre del departamento: ");
    fflush(stdin);
    gets(busqD);

    printf("\n\nID EMPLEADO\tNOMBRE\t\tSALARIO");

    for(int i=0;i<lim;i++){
        for(int j=0;j<9;j++){

            k=strcmp(busqD,pers[i][j]);<--ERROR
            if(k==0){
            printf("\n%i\t %s\t %.2",pers[i][0],pers[i][1],pers[i][8]);
        }
    }
}
    
asked by Anael Dominique 04.03.2018 в 21:35
source

2 answers

0

The problem you have is that the strcmp algorithm you want to use requires two char pointers as two current arguments (two char *) that are the memory address of the two C-style character strings you want to compare.

That's because the second argument you're sending, pers[i][2] is not a string, but a simple character (a char).

I've made a simple code for you to take a look, write "Adrian" or "Pepe" or "Miriam" so you can see how it goes.

If you have any further questions, I'll be here for you.

#include <iostream>
#include <cstring>
using namespace std;
//Compiler version g++ 6.3.0

int main(){
const int MAX = 3;
const char empleados[MAX][50] =
    {
        "Adrian",
        "Miriam",
        "Adrian"
    };

char nombre[50];

cout << "Introduce un nombre";
cin.get(nombre, 50);

for(int i=0; i<MAX; i++){
    if(strcmp(nombre, empleados[i]) == 0){
        cout << "El empleado " << i
        << " " << " tambien se llama "
        << nombre << endl;
    }
}
}
    
answered by 05.03.2018 / 16:09
source
0

You are comparing the first character of both strings in that condition, to make a string comparison you should go through character to character to know if they are equal, but for that procedure there are already solutions in the standard library, for example "strcmp"

int strcmp(const char *s1, const char *s2); 

Compare the string pointed to by s1 with the string pointed to by s2. The function returns a whole number greater, equal, or less than zero, appropriately according to the string pointed by s1 is greater, equal, or smaller than the string pointed by s2.

An example:

int main()
{
   char s1[5] = "Abeja";
   char s2[5] = "abeja";
   int i;

   printf( "s1=%s\t", s1 );
   printf( "s2=%s\n", s2 );

   i = strcmp( s1, s2 );
   printf( "s1 es " );
   if( i < 0 )  printf( "menor que" );
   else if( i > 0 )  printf( "mayor que" );
   else  printf( "igual a" );
   printf( " s2\n" );

   return 0;
}

Your code would be as follows:

#include <string.h>

void listado(char pers[100][9], int& lim){
char busqD[40];
printf("\nCONSULTAS\n\nIngresar el nombre del departamento: ");
fflush(stdin);
gets(busqD);

printf("\n\nID EMPLEADO\tNOMBRE\t\tSALARIO");
for(int i=0;i<lim;i++){
    for(int j=0;j<9;j++){
        if(strcmp(busqD, pers[i][2]) == 0){//son iguales
            printf("\n%i\t %s\t %.2",pers[i][0],pers[i][1],pers[i][8]);
        }
    }
}
    
answered by 04.03.2018 в 21:46