Entry problem in loop comparing string

Asked

Viewed 84 times

4

I can’t make it into the loop using only the char 'Anunciar', when you put something in, it starts the loop. What should I do?

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

int main ()
{
    char nome[50], op;
    int sexo, passatempo, musica, filme, auto_declara;

    printf("Ola, seja bem-vindo ao Colega de Quarto, como voce se chama ? ");
    scanf("%[^\n]s", &nome);
    printf("O que voce deseja, %s ?\n\n", nome);
    printf("---Anunciar---\n\n");
    printf("---Procurar---\n\n");
    scanf("%s", &op);

    do
    {
        printf("\nFormule seu perfil de acordo com as opcoes abaixo: \n");

        printf("\n");

        printf("---Sexo---\n");
        printf("1 - Masculino\n");
        printf("2 - Feminino\n");
        scanf("%d", &sexo);

        printf("\n");

        printf("---Hobby---\n");
        printf("1 - Ler\n");
        printf("2 - Praticar esportes\n");
        printf("3 - Festar\n");
        scanf("%d", &passatempo);

        printf("\n");

        printf("---Gosto Musical---\n");
        printf("1 - Rock\n");
        printf("2 - Funk\n");
        printf("3 - Sertanejo\n");
        printf("4 - Rap\n");
        scanf("%d", &musica);

        printf("\n");

        printf("---Tipo de Filme---\n");
        printf("1 - Acao\n");
        printf("2 - Suspense\n");
        printf("3 - Terror\n");
        printf("4 - Drama\n");
        printf("5 - Comedia\n");
        printf("6 - Ficcao Cientifica\n");
        scanf("%d", &filme);

        printf("\n");

        printf("---Voce se autodeclara ?---\n");
        printf("1 - Preguicosa\n");
        printf("2 - Prestativa\n");
        printf("3 - Caseira\n");
        printf("4 - Festeira\n");
        scanf("%d", &auto_declara);

    } while(op == 'Anunciar');

    return 0;
}
  • You declared your op variable to contain a single character but try to read a string into it and then make a comparison in while by mixing a character with a string but using the character delimiter (a single one, between ') and not a string (between "). Also to compare strings you need to use the strcmp function of <string. h> and not the operator ==.

1 answer

3


The main reason is that you are trying to compare strings with the == when it is right to use the function strcmp(). And there’s another problem that’s not exactly comparing two strings, one of the operands is a poorly formed character, because the use of simple quotes indicates that you have one character, and not a sequence of them, then the rest will be ignored, then the correct one would be:

strcmp(op, "Anunciar")

I put in the Github for future reference.

More details.

But it can still work without being the right way to do it for the user experience. Interestingly, he got the code right and he got it wrong that he needed to assemble menus with option numbers. So the real solution is to number the menu options and buy the number, keep string so much can go wrong, even if it is working (compiling and running).

Browser other questions tagged

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