Language C - The structure jumps directly to ELSE by ignoring IF parameters [duplicate]

-4
#include<stdio.h>

int main(){

    //Escolhe entre nome do personagem e classe

    int p,c;
    printf("RPG teste\n");

    printf("Digite um nome para o seu(a) personagem:");
    scanf("%s",&p);
    printf("Escolha uma classe, digitando entre Guerreiro ou Monge:");
    scanf("%s",&c);

    if (c=="Guerreiro"){
        printf("Guerreiro");
    }
    else
    {
        printf("Monge");
    }
    system("pause");
}
    
asked by anonymous 23.09.2018 / 04:55

2 answers

5

You have to revise the concepts in C a bit, there are some "errors" in your code, such as:

  • The variables p and c are declared as int , but in the code they are used to save a word.

When you use int , you are specifying that the variable will receive an integer, ie you will not be able to save other types of data, not being able to save a word, for example. For this you can specify a variable of type char (string), which will save a total number of characters, for example:

char variavel[50];
  • In if structure, you try to compare c directly with the word "Warrior".

Even if you set the variable type to char (string), as in the example above and try to make a if of direct comparison, it would give "error", it will never enter the condition of if because it is not possible to directly compare a variable of type char (string). Except for exception, where char is used to save a single character, for example:

#correto
char variavel;
variavel = 'a';
if(variavel == 'a');

#incorreto
char variavel[20];
variavel = "palavra";
if(variavel == "palavra");

When using only char variavel , you can save a single character, allowing you to make a comparison in a if normal with single 'a' , but when using char variavel[20] , you are declaring a string that can save a number of characters in it, depending on the value you specify in the assignment.

In this case the comparison is different, there is a function in the <string.h> library that makes this comparison, this function is strcmp() , you can see a better explanation on this page . Basically, it returns a number depending on the situation of the comparison, being able to check if it is greater, equal or smaller:

c = "variavel";
if(strcmp(c, "variavel") == 0); #a função retorna 0, pois são iguais

So you can make a comparison between two words.

  • Use scanf() to read words.

Although it works, it is not appropriate to use scanf() to read words, as it will not be able to read a compound name, for example:

scanf("%s", variavel); #foi digitado o nome José Maria
printf("%s", variavel); #imprime somente o nome José

So, the most appropriate in this situation is to use gets() , which will save everything typed, you can read more about it here .

Last but not least is the complete code, as it should be:

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

main(){
    char p[20];
    char c[20];
    printf("RPG TESTE\n");
    printf("Digite um nome para o seu(a) personagem:");
    fflush(stdin);
    gets(p);
    printf("\nEscolha uma classe, digitando entre Guerreiro ou Monge:");
    fflush(stdin);
    gets(c);
    if(strcmp(c, "Guerreiro") == 0){
        printf("\n\nGuerreiro");
    }
    else {
        printf("\n\nMonge");
    }
}
    
23.09.2018 / 06:43
1

Hello, Danilo! Welcome!

The solution to your problem involves some important concepts of structured C programming.

A variable is a space in memory. It has name, address and can store a value.

When you declare a variable, you must specify the type of variable you want.

  • int : integer values (-2, 0, 1)
  • float : floating values (-3.5, 1.0, 5.33)
  • double : large floating values (3.1415926535897932384626433832795)
  • char : characters ('a', 'b', 'c')
  • bool : Boolean values (true, false)

When you want to store a word (string, string ), you must use a vector. Vector is a sequence of spaces in memory. Each space is called the index, which starts from 0 (zero). The zero index is the default address of the vector.

char nome[4];
nome[0] = 'A';
nome[1] = 'N';
nome[2] = 'A';
nome[3] = '
char nome[] = "ANA";
char nome[4] = "ANA";
char nome[4] = {'A','N','A'};
char nome[10] = "ANA";
';

The value '''' is used to indicate the end of the string , so it is necessary to reserve a space in the vector.

There are other ways to initialize a string .

int num = 0;
scanf("%d", &num);
printf("numero: %d\n", num);

char nome[10];
scanf("%s", nome);
printf("nome: %s", nome);

Solved characters are assigned between single quotation marks "" , whereas strings are assigned double quotation marks stdio.h .

The scanf(arg_1, arg_2) library has read (keyboard input) printf(arg_1, arg_3) and write (output on the screen) &num functions.

Each function has two arguments, but there are 3 types of arguments, in this case.

  • arg_1: formatting the value
  • arg_2: memory space address
  • arg_3: name of memory space

To read or write a value, you must specify the format of that value.

  • % d: integers and booleans (1 - true, 0 - false)
  • % f: floating
  • % fl: large floats
  • % c: character
  • % s: string

Note that boolean variables return 1 or 0, so they share the same format specifier as integers. For floats, in particular, it is possible to specify the number of houses before and after the point, for example:% 2.3f for numbers 0.333, 25.587, 4.444.

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

    int main(){

        //Escolhe entre nome do personagem e classe

        char p[20];
        char c[20];
        printf("RPG teste\n");

        printf("Digite um nome para o seu(a) personagem:");
        scanf("%s",p);
        printf("Escolha uma classe, digitando entre Guerreiro ou Monge:");
        scanf("%s",c);

        if (strcmp(c,"Guerreiro") == 0){
            printf("Guerreiro");
        }
        else
        {
            printf("Monge");
        }
        return 0;
    }

Note that to read from the keyboard an integer value, the address entered was nome ("e" followed by the variable name). Already to read a string , the address entered was strcmp(string_1, string_2) (just the vector name). This is done because the vector address is bound to the vector name itself, pointing to the vector zero index.

Now we are going to solve the problem in question.

To compare if two strings are the same, it is necessary to use the string string.h function in the strcpy(Nome, "nome de alguém") library.

char nome[4];
nome[0] = 'A';
nome[1] = 'N';
nome[2] = 'A';
nome[3] = '
char nome[] = "ANA";
char nome[4] = "ANA";
char nome[4] = {'A','N','A'};
char nome[10] = "ANA";
';

Bonus: Another function of the string.h library that may be useful is %code% .

    
23.09.2018 / 08:18