0

I'm using Windows and CodeBlocks IDE by the way.

If my code was( has space before the %d specifier):

    #include <stdio.h>

int main(void)
{
    int num;
    printf("Enter a number you want to check is present: ");

       while(scanf(" %d",&num)==EOF) // As long as wrong input is present on buffer
       {
          printf("Please enter valid input...\n");
          //clear the input buffer
       }
    return 0;
}

I've have to type in Ctrl+Z twice(once on each line,doesn't have to be at the beginning of line) for the while(scanf(" %d",&num)==EOF) case to run.

Whereas if my code was: If my code was(no space before the %d specifier):

    #include <stdio.h>

int main(void)
{
    int num;
    printf("Enter a number you want to check is present: ");

       while(scanf("%d",&num)==EOF) // As long as wrong input is present on buffer
       {
          printf("Please enter valid input...\n");
          //clear the input buffer
       }
    return 0;
}

I've have to type in Ctrl+Z once for the while(scanf("%d",&num)==EOF) case to run.

So there are 2 parts in this question:

1)Why do I need to press Ctrl+Z twice for the first example? I tried to refer to https://stackoverflow.com/a/21261742/10701114 but it was for UNIX and kind of confusing for me

2) Why does the space before the %d specifier even cause such a discrepancy since it shouldn't matter unless its a %n,%c or %[...] (I got this from https://stackoverflow.com/a/36504282/10701114)?

NOTE: If I should post these 2 parts as 2 questions, please do comment.

Update : I tried this on the cmd prompt with gcc(my first time on a cmd prompt), the same issue persists

Leon
  • 346
  • 3
  • 15
  • 1
    In the Windows console (the "DOS prompt") you must press `Ctrl-Z` on an empty line for it to be detected as `EOF`. – Some programmer dude Apr 20 '20 at 02:19
  • On CodeBlocks,even if its an empty line, I still have to press `Ctrl-Z` twice. ( I pressed `Enter` to go to a newline and pressed `Ctrl-Z` once, it didn't work till I pressed another `Ctrl-Z` ) . Does it only work properly on the Windows console? – Leon Apr 20 '20 at 02:26
  • 1
    It's possible that the console in your IDE doesn't follow the same rules as the Windows console. You should try running it outside of the IDE in a normal Windows console and verify its behavior there. – MemReflect Apr 20 '20 at 02:36
  • 1
    Should the 2nd "I've have to type in Ctrl+Z once for the `while(scanf(" %d",&num)==EOF)` case to run." --> "I've have to type in Ctrl+Z once for the `while(scanf("%d",&num)==EOF)` case to run."? (no space) – chux - Reinstate Monica Apr 20 '20 at 02:38
  • And the UNIX stuff won't apply to anything on Windows. Windows doesn't even know what a TTY is, and its EOF behavior is as stated by @Someprogrammerdude, which is different from behavior in UNIX TTYs (virtual or otherwise). – MemReflect Apr 20 '20 at 02:39
  • 1
    The it could be a buffering issue of Code:Blocks itself and how it handles input. – Some programmer dude Apr 20 '20 at 02:39
  • Leon, Please clarify that there is no prior I/O function and you provided no prior input. – chux - Reinstate Monica Apr 20 '20 at 02:40
  • @chux-ReinstateMonica What I pasted is actually the entire program – Leon Apr 20 '20 at 02:44
  • Loen, "actually the entire program" --> no `main()`? – chux - Reinstate Monica Apr 20 '20 at 02:46
  • As in I didn't paste everything – Leon Apr 20 '20 at 02:49
  • @MemReflect I currently don't have a compiler that doesn't rely on an IDE. I will get back asap once I try it on the windows console – Leon Apr 20 '20 at 03:08
  • `int num, rtn; ... while (1) { rtn = scanf ("%d", &num); if (rtn == EOF) { /* handle EOF */} else if (rtn == 1) { /* good input, validate range, clear remaining, break */ } else { /* invalid input - matching failure */ }}` – David C. Rankin Apr 20 '20 at 04:03
  • @DavidC.Rankin, the problem still persists with your code – Leon Apr 21 '20 at 11:49
  • @Leon You understand why? When you have, e.g. `"abcd"` and press [Ctrl+z], you signify end-of-input for`"abcd"` and that is taken as your input, `EOF` is not returned. `scanf()` then blocks again waiting for input. On the second press of [Ctrl+z], there is no input and an *input failure* occurs and `scanf()` then returns `EOF` breaking the loop. The code I posted was to show to handle all 3 possible conditions you must validate to use `scanf()` correctly. – David C. Rankin Apr 21 '20 at 17:26

0 Answers0