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;
}