Why is my code always entering the if condition?

69 Views Asked by At
#include <stdio.h>

#include <stdbool.h>
//functions
bool check_letter_validity(char);

int main() {
    int score=0;
    char c=0,x=0,previous_value=0;
    printf("Please Enter Cool Letter : \n");
    scanf(" %c", &c);
    printf("Please Enter Text : \n");
    while (scanf(" %c", &x) == 1 ){
        if (check_letter_validity(x)==false) {
            score = -1;
            break;
        }
    
        if (x == c || x == c-('a'-'A')){
            score++;
            if (previous_value == c || previous_value == c-('a'-'A'))
                score++;
        }
        previous_value = x;
    }
    printf("The score is: %d", score);
    return 0;
}

bool check_letter_validity(char x){
    if (x==' ')
        return true;
    if (x<='z' && x>='a')
        return true;
    if (x>='A' && x<='Z')
        return true;
    else
        return false;
}

This is a code I wrote in which I receive a "cool letter" such as b and then receive a text such as "bhkb kBfj" and I have to scan the text for the letter 'b/B' and each b that is scanned increases "score" by one point.

Anyway, the problem is I have an if statement that is always true. For some reason, the if statement is always true even though it is supposed to be true only when chars that aren't English letters or spaces appear. Why is it the if statement is always correct? The output I receive:

Please enter cool letter:
(input) b.
please enter text:
bhkb kBfj
the score is: -1.

The score in this example is supposed to be 2, but it seems at some point the if statement returned false even though i haven't entered any false letters (such as *,%).

0

There are 0 best solutions below