Can anyone tell me why the EOF (allowing the user to enter F6 or Ctrl Z) may not be working for me? I have a loop running "while(input != EOF)". Any help is greatly appreciated.
This is a discussion on Need Help With EOF within the C Programming forums, part of the General Programming Boards category; Can anyone tell me why the EOF (allowing the user to enter F6 or Ctrl Z) may not be working ...
Can anyone tell me why the EOF (allowing the user to enter F6 or Ctrl Z) may not be working for me? I have a loop running "while(input != EOF)". Any help is greatly appreciated.
I can't tell without seeing more code, it could be that you're trying to read characters and your input variable is declared as char. Since EOF has a value that can't be held by char you would need to use int instead.
Post your code and I'll be able to tell you more
-Prelude
My best code is written with the delete key.
Below is my code from main, I have functions below it that I did not copy here, but this will show you what I am trying to do. I appreciate the help!
//Function Prototypes
void display_header();
int main()
{
//Declare variables
char input;
char operation;
double num1;
double num2;
double result;
//Prototype functions
double product(double, double);
double quotient(double, double);
double sum(double, double);
double difference(double, double);
//Call function
display_header();
//Prompt user to begin
printf("Press enter to begin the program.");
fflush(stdin);
scanf("%c", &input);
fflush(stdin);
while (input != EOF)
{
//Clear screen
system("cls");
//Prompt user for numbers
printf("Enter your first number: ");
scanf("%lf", &num1);
fflush(stdin);
printf("Enter your second number: ");
scanf("%lf", &num2);
fflush(stdin);
//Clear screen
system("cls");
//Prompt user for operation
printf("Choose:\n");
printf("'*' for the product\n");
printf("'/' for the quotient\n");
printf("'+' for the sum\n");
printf("'-' for the difference\n\n");
printf("What operation would you like to perform: ");
fflush(stdin);
scanf("%c", &operation);
//Evaluate operation chosen and call appropriate function
if (operation == '*')
result = product(num1, num2);
else if (operation == '/')
result = quotient(num1, num2);
else if (operation == '+')
result = sum(num1, num2);
else if (operation == '-')
result = difference(num1, num2);
else
printf("Invalid choice!\n\n");
//Prompt user to continue
printf("Press enter to continue program.");
fflush(stdin);
scanf("%c", &input);
}
return 0;
}
>char input;
change that to
int input;
and you should be able to use EOF. EOF is a value too big for a char, so to test for it you need to use an int. It's a tough bug to find unless you know what you're looking for. Kinda goofy, but that's life![]()
-Prelude
My best code is written with the delete key.