-1

Get input untill it's an integer beetween 0 and 23.

int n;    
do
{
    printf("Enter height: ");
    scanf("%d", &n);
} while ( n < 0 || n > 23);

adding EOF and NULL to while somehow?

potarin
  • 3
  • 2

1 Answers1

0

The only thing that I see is that the input '24' itself won't stop the loop, so you need to change the condition to:

while ( n < 0 || n > 24);

NULL is not an issue, as you can't get NULL from stdin. NULL means an address that points byte number 0. It won't happen, as you call scanf with a memory that is on the stack of the current running thread.

about EOF from stdin, you can get more info here End of File(EOF) of Standard input stream (stdin).

About using scanf, I would recommend to read: Disadvantages of scanf.


EDIT: This is what's best to do. Get rid of scanf and use fgets and strtol, like the following example (taken from This SO answer)

char *end;
char buf[LINE_MAX];
int n = 0;

do {
    if (!fgets(buf, sizeof buf, stdin))
        break;

    // remove \n
    buf[strlen(buf) - 1] = '\0';

    //Get integer from line
    n = strtol(buf, &end, 10);
 // making sure that a valid input was given (i.e. a number) and its value
 // You might also want to check that strlen(end) is zero, to avoid input like "213 abcde", depends on you
} while ((end != buf) && ( (n > 24 || n < 0)));
Noam Ohana
  • 186
  • 14
  • my problem is that I don’t know how to ask for input again when I got " ", "abc" or enter key. it loops – potarin Nov 29 '17 at 11:28