Problem in a C entry

1

Someone could explain or say, Why does not the code enter? If 0 comes out, it only ends.

#include <stdio.h>  
#include <stdlib.h>
#include "dibujos.h"
#include <time.h>  
int main(){
    char pal_1 [12] = "hogar";          
    char let;                                  
    int vidas = 0;                             
    srand(time(NULL));

    system("cls");
    TituloJuego();
    SinAhorcado();
    printf("\n\n");
    system("pause");
    int pal = rand() % 10;                    
    printf("%d" , pal);
    if(pal = 0){
        while (vidas !=0){
        system("cls");
        TituloJuego();
        SinAhorcado();
        printf("\n\n");
        printf("Palabra secreta:_ _ _ _ _ \n");
        printf("Ingrese una letra: ");
        scanf("%c", &let);
        if( let = "b" || "B" || "c" || "C" || "d" || "D" || "e" || "E" ||
            "f" || "F" || "i" || "I" || "j" || "J" || "k" || "K" || "l" ||
            "L" || "m" || "M" || "n" || "N" || "p" || "P" || "q" || "Q" ||
            "s" || "S" || "t" || "T" || "u" || "U" || "v" || "V" || "w" || 
            "W" || "x" || "X" || "y" || "Y" || "z" || "Z"
        ){
            system("cls");
            TituloJuego();
            Cabeza();
            printf("\n\n");
            system("pause");
            printf("hola");
        }
    }   
}
    
asked by Thasdjl 26.09.2016 в 23:30
source

1 answer

3

Let's start with the basics:

The comparisons in C are made in the following way:

if (izq <operador> der) { /* ... */ }
O:
if (true||false) { /* ... */ }

Where:

  • izq : It is the operand in the left hand, a variable or a value to be compared.
  • <operador> : Can be any of the comparison operators:

    • <= : Less than or equal to what
    • < : Less than
    • == : Same as
    • != : Different from
    • > : Greater what
    • >= : Greater than or equal to what
  • der : Value or variable that we expect to have (or not) on the left side.

  • What you are currently comparing:

    if (let = "string" || "otro string")
    Y:
    if (pal = 0) 
    

    It should cause a compile-time error, since let is type char and not char* , although I'm not very good with C, so I can not comment on it, the chars are enclosed within single quotes ( ' ), and not double ( " ) , currently, if compiled, but it turns out that it evaluates any string of characters, so it will always be true.

    On the subject of assignment in a comparison, it is badly raised, by doing:

    if (pal = 0)
    

    You are assigning zero to pal , which in the end translates to the following comparison:

    if (0)
    

    And in C, 0 means false.

    Another thing, scanf("%c", /* ... */); is waiting for a type char , so you can not compare let with a char array, instead, you can compare as follows:

    if ((let >= 'b' && let <= 'z') || (let >= 'B' && let <= 'Z')) {
        /* ... */ 
    }
    

    Ah! The vidas , I believe ... will always be zero, so it will never enter the cycle while , and if it is different from zero, there is no method or action that allows the change of value of that variable.

    Then, leaving all the code:

    int main(){
        char pal_1 [12] = "hogar";          
        char let;                                  
        int vidas = 5;                             
        srand(time(NULL));
    
        system("clear");
    
        int pal = rand() % 10;                    
        printf("%d\n" , pal);
        if(pal == 0) {
            while (vidas != 0) {
                system("clear");
                printf("Palabra secreta:_ _ _ _ _ \n");
                printf("Ingrese una letra: ");
                scanf("%c", &let);
    
                if((let >= 'b' && let <= 'z') || (let >= 'B' && let <= 'Z')) {
                    printf("hola");
                }
            }
        }
    }
    

    I have removed all the printing functions (And the others that you have put because I would not compile if I left them).

    I hope it has helped you: D

        
    answered by 27.09.2016 в 01:25