Doubt with bool in C

Asked

Viewed 691 times

-2

Well, I did this vector test, but I’m having a problem with the bool type, which never turns false and displays the message of not found.

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

int main() {

    char *palavras[] = {"maca", "uva", "pera"};
    int tamanho = sizeof(palavras) / sizeof(char *);
    int tam = tamanho;
    char nome[100];
    bool achou = false;

    while(true) {
    if(tam > 0) {
    printf("[ ");
    for(int i=0; i<tam - 1; i++) {
        printf("%s, ", palavras[i]);
    }
       printf("%s ]\n", palavras[tam - 1]);
    }
    printf("Deseja excluir qual fruta? ");
    fgets(nome, 20, stdin);
    nome[strlen(nome) - 1] = '\0';
    if(tam == 0) { puts("Sem frutas disponiveis"); break; }
    for(int i=0; i<tam; i++) {
    achou = false;
    if(!strcmp(nome, palavras[i])) {
        printf("Fruta: %s, excluida.\n\n", nome);
        for(int j=i; j<tam - 1; j++) {
            palavras[j] = palavras[j + 1];
        }
        tam--;
        achou = true;
        break;
     }
    if(achou) { //se falso
        printf("Fruta: %s, nao encontrada.\n\n", nome);
    } } }

    return 0;
}

I can’t figure it out.

  • Why you remove the last letter of the name of the fruit you enter, in line 24?

  • Are you referring to fgets?

  • The line immediately below the fgets()

  • name[strlen(name) - 1] = ' 0'; but this will remove the ' n', without it the comparison would not work.

  • You are right; it is the gets() that did not inculcate the return car in the string that returns. I compiled your code here and it worked correctly, however. There seems to be no error in it.

  • if (achou) { // se falso shouldn’t be if (!achou) { // se falso ? And maybe you should be out for that if.

Show 1 more comment

1 answer

-1


I edited your program

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

int main()
{

  char *palavras[] = {"maca", "uva", "pera"};
  int tamanho = sizeof(palavras) / sizeof(char *);
  int tam = tamanho;
  char nome[100];
  bool achou = false;
  while(true)
    {
      if(tam > 0)
        {
          printf("[ ");
          for(int i=0; i<tam - 1; i++)
            {
              printf("%s, ", palavras[i]);
            }
          printf("%s ]\n", palavras[tam - 1]);
        }
      printf("Deseja excluir qual fruta? ");
      fgets(nome, 20, stdin);
      nome[strlen(nome) - 1] = '\0';
      if(tam == 0)
        {
          puts("Sem frutas disponiveis");
          break;
        }
      for(int i=0; i<tam; i++)
        {
          achou = false;
          if(!strcmp(nome, palavras[i]))
            {
              printf("Fruta: %s, excluida.\n\n", nome);
              for(int j=i; j<tam - 1; j++)
                {
                  palavras[j] = palavras[j + 1];
                }
              tam--;
              achou = true;
              break;
            }
        }
      if(!achou) // retirado de dentro do for e na negativa
        { //se falso
          printf("Fruta: %s, nao encontrada.\n\n", nome);
        }
    }
    return 0;
}
  • Hmm, so despite declaring the found variable = false; in default if will be true?

  • if you find the word, it is true. If you do not find it, it is false. As it is (!found), then if you find the word, you do not enter if, and if you do not find it, you enter if.

  • wanted to understand why the -1?

  • This site is full of arrogant people.

  • So far, I understand the concern of the guys to keep a site clean, with questions and answers respecting the format tour. The problem is negative and we don’t even know where we are wrong. Yes, to err is human.

  • @lemoce , I believe that your negativity has been given because you have not explained the answer, only presented a modified code.

  • 1

    I’ll edit it. Thanks @Jeffersonquesado.

Show 2 more comments

Browser other questions tagged

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