Thread: Need Help With EOF

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Need Help With EOF

    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.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    2

    Prelude -- Portion of Code

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

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 09:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 04:04 PM
  5. files won't stop being read!!!
    By jverkoey in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2003, 05:28 AM