I can’t use strcmp in C

Asked

Viewed 48 times

-4

I need to show a student’s chart using the license plate, as I do?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main() {

  struct ficha {
    char matricula[9], inf[9];
    char nome[50];
    char curso[20];
    char periodo[10];
  };
  struct ficha alunos[2];

  int i;

  for (i = 0; i < 2; i++) {
    printf("\nInforme a matricula do aluno: ");
    scanf("%s", & alunos[i].matricula);

    printf("\nInforme o nome do aluno: ");
    scanf("%s", & alunos[i].nome);

    printf("\nInforme o curso do aluno: ");
    scanf("%s", & alunos[i].curso);

    printf("\nInforme o periodo do aluno: ");
    scanf("%s", & alunos[i].periodo);
  }

  printf("\n\nDigite uma das matriculas para informacoes de um aluno:");
  scanf("%s", & alunos[i].inf);

  if (strcmp(alunos[i].matricula, alunos[i].inf) == 0) {
    printf("\n\n---------------------------Informacoes---------------------------\n\n");

    printf("\nMatricula: %s \n", alunos[i].matricula);
    printf("\nNome: %s \n", alunos[i].nome);
    printf("\nCurso: %s \n", alunos[i].curso);
    printf("\nPeriodo: %s \n", alunos[i].periodo);

  } else
    printf("Aluno nao encontrado");
  system("pause");
}
  • scanf("%s", & alunos[i].inf); note in your code that this line is outside the for, ie, will use the last value you have in the variable "i". this property "inf" would not need to be in struct if it will read only once, it could be a common variable. And it should then do a for to find this value and see if it was typed

1 answer

-2


As I put in the comment, you’re using scanf("%s", & alunos[i].inf); outside the for, that is, will read a value in "i" with the last value of for and that comparison won’t work.

The searched value can be a separate variable, which does not need to be in the struct since you won’t read inside the for, so for example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main() {

  struct ficha {
    char matricula[9];
    char nome[50];
    char curso[20];
    char periodo[10];
  };
  
  struct ficha alunos[2];
  
  char pesquisa[9];
  bool achou = false;

  int i;

  for (i = 0; i < 2; i++) {
    printf("\nInforme a matricula do aluno: ");
    scanf("%s", & alunos[i].matricula);

    printf("\nInforme o nome do aluno: ");
    scanf("%s", & alunos[i].nome);

    printf("\nInforme o curso do aluno: ");
    scanf("%s", & alunos[i].curso);

    printf("\nInforme o periodo do aluno: ");
    scanf("%s", & alunos[i].periodo);
  }

  printf("\n\nDigite uma das matriculas para informacoes de um aluno:");
  scanf("%s", & pesquisa);

  for (i = 0; i < 2; i++) {
    if (strcmp(alunos[i].matricula, pesquisa) == 0) {
        printf("\n\n---------------------------Informacoes---------------------------\n\n");

        printf("\nMatricula: %s \n", alunos[i].matricula);
        printf("\nNome: %s \n", alunos[i].nome);
        printf("\nCurso: %s \n", alunos[i].curso);
        printf("\nPeriodo: %s \n", alunos[i].periodo);
        achou = true;
    }
  }
  
  if (achou == false) {
       printf("\nAluno nao encontrado");
  }

  system("pause");
}

Note that, after reading the value, the strcmp is inside a for to compare with each value that was entered, until finding. In case you don’t find it, I created the variable "found" that will have the initial value "false" and will change to "true" if you find.

At the end has the parole if (achou == false) to display the message if not found, which could also be if (!achou)

  • I had never used this bool, but I think I understood how the code worked, thank you!

  • pruner be tbm int and zero be the "false" and a the "true" :)

Browser other questions tagged

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