Relate String matrix to a Float vector in C

Asked

Viewed 25 times

1

I need the program to receive 20 names typed by the user, store in a Char Matrix, and right after receive the 20 salary of the users and store in a Float vector. I was able to do even the part where the user should use the program until requesting the display of an invalid Index (0 to 19 are valid); because the Char matrix does not accept to display the data through the Index, at least I could not display it like this.

Can someone help me?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
   float sal[20];
   char nom[21][16];
   int index, n, s;
   //recebe os nomes
   for(n = 0; n < 21; n++){
           printf("Insira o nome do funcionario [%d] \n", n);
           gets(nom[n]);
   }
   //exibe os nomes
   for(n = 0; n < 21; n++){
           printf("%s", nom[n]);
       printf("\n");   
   }
   //recebe os salários
   for(s = 0; s < 20; s++){
       printf("Insira o salario do funcionario [%d] \n", s);
       scanf("%f", &sal[s]);
   }

   //exibe o nome e salário de acordo ao índice digitado pelo usuário
       while(index < 20)
   {
       printf("Indice escolhido: Funcionario %s %s ", nom[index][index]);
       printf("Salario %f ", sal[index]);
       printf("\n");
       printf("Qual o indice do Vetor que deseja consultar? \n");
       scanf("%d", &index);    
   }
   
   return 0;
} ```
  • Solved, the solution was quite simple in fact: within the loop WHILE instead of using printf to display the nom[index], just use the command puts(nom[index] that displays smoothly.

No answers

Browser other questions tagged

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