Linxy Linxy - 4 months ago 15
C Question

If statement is automatically going to else even if the condition is good

My code:

#include <cs50.h>
#include <stdio.h>

// Height < 24

string block;

int main(void) {
printf("Height: ");
int height = GetInt();
while (true) {
if (height < 24 && height < 0) {
for (int a = 1; a < height; a++) {
block = "#";
printf("%s\n", block);
}
} else {
printf("Height: ");
height = GetInt();
}
}
return 0;
}


The way it works is it asks for a "Height". If this height is less than 24 or greater than 0 than it will print that height of "#"s, else it will ask for another height to fit the criteria. However, my if statement, no matter what I put as the height will go to the else statement, for what ever reason.

EDIT: Apparently I'm dumb as bricks. Thanks for the help guys.

Answer

If this height is less than 24 or greater than 0 than it will print that height of "#"s, else it will ask for another height to fit the criteria.

You need:

if (height < 24 && height > 0) {

> 0 instead of < 0.