3

I'm having trouble using EOF adequately.

what I want: When the program executes it should automatically prompt the user to enter a number and exit when the user signals EOF through the keyboard

what's actually happening: when I run the program, it sits there waiting for the user to hit enter THEN it prompts the user to enter a number; this somehow makes my code buggy


#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> // For exit() 

int main( ) {

    char c;
    while ( (c = getchar()) != EOF)  {

        int user_numbr = 0;
        int file_number = 0;
        int last_Appearance = 0;
        int index = 0;
        bool notFound;

        FILE *fptr;
        fptr = fopen("numbers.text", "r"); 

        printf("Enter a number: ");
        scanf("%d", &user_numbr);


        while ( !feof (fptr) ) {

            fscanf (fptr, "%d", &file_number);
            if ( feof (fptr) ) break;
            index++;

            if ( user_numbr == file_number ) {
                last_Appearance = index;
            }

        }
        fclose(fptr);

        if ( last_Appearance != 0) {
            printf("%d last appears in the file at position ", user_numbr);
            printf("%d\n", last_Appearance);
        }
        else if ((c = getchar()) != EOF)
            printf("%d does not appear in the file\n", user_numbr);

    }

    return 0;
}
CharithJ
  • 46,289
  • 20
  • 116
  • 131
jay to
  • 37
  • 1
  • 1
    `char c` should be `int c`. Also [while ( !feof (fptr) )](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) is wrong. You should check the return value of fscanf – MFisherKDX May 20 '19 at 23:57
  • 2
    `getchar()` blocks until the user hits a key. And that's your very first line of executable code. It's before your prompt. – zzxyz May 20 '19 at 23:59
  • You need to flush if you expect "Enter a number: " to be written. To see when the input stream is closed (or, in horribly ambiguous/incorrect parlance "when EOF is sent"), just check the return value of scanf. – William Pursell Feb 18 '20 at 01:25

2 Answers2

0

Assuming that you want the user should enter the file number only once, you should probably move

printf("Enter a number: "); scanf("%d", &user_numbr);

above the while loop.

Tejus
  • 694
  • 1
  • 7
  • 19
  • the program must repeatedly ask user for a number until the user signals end of file by pressing (CTRL + D or CTRL + Z) – jay to May 21 '19 at 00:18
  • Please have a look at https://stackoverflow.com/questions/4358728/end-of-file-eof-in-c – Tejus May 21 '19 at 00:30
  • EOF signifies end of file. The program should be able to identify the end of file. Why do you want the user to intervene by pressing Ctrl+D or Ctrl+Z. Instead, you can use break statement to get out of the loop. – Tejus May 21 '19 at 00:40
  • this is an assignment and that's one of the requirements but no worries i was able to fix it. ```while ( !feof(stdin) ) ``` – jay to May 21 '19 at 02:22
  • this is also the same condition for the else if at the bottom. – jay to May 21 '19 at 02:31
-1

I found a fix. I'll post it if anyone who has this problem see's this post.

corrections: 1)first while loop condition 2)last else if condition

int main( ) {

    char c;
    while ( !feof(stdin) )  {

        int user_numbr = 0;
        int file_number = 0;
        int last_Appearance = 0;
        int index = 0;
        bool notFound;

        FILE *fptr;
        fptr = fopen("numbers.text", "r"); 

        printf("Enter a number: ");
        scanf("%d", &user_numbr);


        while ( !feof (fptr) ) {

            fscanf (fptr, "%d", &file_number);
            if ( feof (fptr) ) break;
            index++;

            if ( user_numbr == file_number ) {
                last_Appearance = index;
            }

        }
        fclose(fptr);

        if ( last_Appearance != 0) {
            printf("%d last appears in the file at position ", user_numbr);
            printf("%d\n", last_Appearance);
        }
        else if ( !feof(stdin) )
            printf("%d does not appear in the file\n", user_numbr);

    }

    return 0;
}
jay to
  • 37
  • 1
  • Downvoting because this is wrong. See [my comment](https://stackoverflow.com/questions/56229458/asking-the-user-for-values-until-they-signal-end-of-file#comment99078860_56229458). All issues still apply – MFisherKDX Jun 19 '19 at 03:29