3

On pset1 I'm trying to make a do ... while loop to get the user's input for the pyramid height for Mario. I swear it should be simple...

This is my source code for dowhile.c:

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

int main(void)
{
printf("Please enter the half pyramid's height\n");
int pyramidHeight;

do {
    pyramidHeight = GetInt();
    } while (pyramidHeight < 0 || > 23);

}

What I think this should mean is something like: Do GetInt (i.e., keep asking for the user's input) and if their input is less than 0 OR greater than 23, it should GetInt again.

(i.e., pyramidHeight must be 0 - 23 to move to the next bit of code)

Every time I try to compile it though I get this error:

dowhile.c:11:40: **error: expected expression**
    } while (pyramidHeight >= 0 && <= 23);

I'm sure I must be doing something very basic wrong here, can anyone please help?

3

You have to explicitly state the comparison each time:

you have

while (pyramidHeight >= 0 && <= 23);

and it should be

while(pyramidHeight <= 0 && pyramidHeight >=23);

Remember C needs you to explicitly state everything!

Also, it's probably good form to initialize your pyramidHeight variable to 0 to start with, just for good measure.

| improve this answer | |
2

while (pyramidHeight >= 0 && <= 23);

As you've seen, that syntax doesn't work. You need to include the variable in both conditions.

An example

while (value < 0 || value > 10)

will run the loop if the user enters any value that isn't between 0-10.

You want the while condition to be the values that cause the loop to be repeated.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .