Print all results of a vector with a given value

Asked

Viewed 234 times

2

I am starting the studies in C. I have a question of how to go through a vector looking for a value and print showing with printf all values found.

The program I’m doing: http://pastebin.com/X2tev5is

In the program above, it would be case 5, where I want to show all employees by the office code. Thanks in advance

  • What’s the big deal?

  • By my code, when I press it to display employee information by position, it shows only one employee. I want to show all employees with a certain position

1 answer

1


I could follow that logic:

case 5:
     //MOSTRA POR CARGO
     printf("Digite o codigo do cargo:\n");
     printf("\n 100\tEngenheiro\n 200\tMestre de Obras\n\300\tPedreiro\n 400\tEstagiario\n");

     scanf("%i", &pesquisa);
     ok = 0;
     // Avisa se o cargo pesquisado nao existe
     if ((pesquisa != 100) && (pesquisa != 200) && (pesquisa != 300) (pesquisa != 400)){
        printf("Pesquisa invalida!");
     }
     else {
        // Loop para percorrer o vetor cargo e verificar a pesquisa
        for(j = 0; j < c; j++) {
           if(pesquisa==cargo[j]) {
              ok = 1;
              printf("\n***********************************\n");
              printf("Funcionario numero %i\n", j);
              printf("Matricula %i\n", matricula[j]);
              printf("Cargo:%i\n", cargo[j]);
              printf("Salario: $ %.2f\n", saldo[j]);
           }
        }
     }

     // Se nao encontrar nenhum funcionario pela pesquisa
     if (ok == 0){
        printf("Sem resultado!\n");
     }
break;
  • Thank you very much!

  • It keeps showing only 1 function. I wanted that when registering 2 employees with code 100 (post), the function showed the 2 employees registered with code 100

  • Strange I tested this morning and it was working, soon I test again.

  • Sorry, it worked. It was my mistake, I checked again here. Mt thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.