4

I'm new to programming and was hoping I could get some help. I'm attempting to create a 'do while' loop in C as part of my CS50 problem sets. I'm attempting to use two different conditions under 'while', but for some reason they are not being recognised. Here's my code

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

int get_height(void);

int main(void)

{
    int height = get_height();
    printf("you picked %i\n", height);
}

int get_height(void)
{
    int height;
    do
    {
        height = get_int("enter height\n");
    }
    while (height > 8 && height < 1);
    return height;
}

I'm trying to force the user to type an integer under 8, but above 0. If I take out either one of the two conditions, that single condition works. If I try to use both conditions however, the code compiles and runs but it doesn't force me to enter a number within the parameters specified. I can just enter any number and it will be accepted. Can anyone help?

EDIT: I'm embarrassed. I misunderstood how AND operators work.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Disierd
  • 53
  • 4
  • 1
    How will it ever be true that `height > 8` and `height < 1` ? – Lee Daniel Crocker Jun 19 '20 at 22:00
  • No point in being embarrassed. But do click the check mark next to one of the answers, so that everyone knows that the question has been answered. See [What should I do when someone answers my question](https://stackoverflow.com/help/someone-answers) – user3386109 Jun 19 '20 at 22:11
  • @user3386109 thanks, I was trying to do it shortly after the first reply but couldn't click it for a certain amount of time. Clicked it now! – Disierd Jun 19 '20 at 22:28

3 Answers3

3

Your condition says "continue the loop if height is larger than 8 AND height is smaller than 1". That condition will never be true, so the loop exit after the first iteration.

What you want is "continue the loop if height is larger than 8 OR height is smaller than 1"

while (height > 8 || height < 1);
dbush
  • 205,898
  • 23
  • 218
  • 273
2

These are mutually exclusive:

while (height > 8 && height < 1);

You probably want:

while (height > 8 || height < 1);

Now if the value is greater than 8 or smaller than 1, the loop will keep going.

bhristov
  • 3,137
  • 2
  • 10
  • 26
1

I think that mathematics have not yet found an integer number that can be at the same time less than 1 and greater than 8.

while (height > 8 && height < 1);

It is evident you mean

while (height > 8 || height < 1);

But to make such a condition more clear for yourself it is useful sometimes to write at first the condition for which a number is being searched.

You need a number that lies in the range [1, 8] that is the target number shall satisfy the condition

1 <= height && height <= 8

In this case the condition in the do-while statement will be a negation of the condition above.

while ( !( 1 <= height && height <= 8 ) );

Now if to remove the negation you will have

while ( !( 1 <= height ) || !( height <= 8 ) );

that is equivalent to

while ( 1 > height || height > 8 );

So either use

while ( !( 1 <= height && height <= 8 ) );

pr

while ( 1 > height || height > 8 );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335