Thread: help with EOF

  1. #1
    Registered User
    Join Date
    Feb 2009

    help with EOF

    hello, i am working on this program that calculates the resistance of an arbitrary number of floating points, the input needs to end when i press zero or EOF...i have the zero part but for some reason i can't get the EOF part....
    here is my code

    #include <stdio.h>
    #define   SENTINAL   0
    int main()
       double r1;
       double resistance = 0.0;
       printf("Enter the resistance or zero to end input:\n");
       while ( 1 )  {
          scanf("%lf", &r1);
          if ((r1 == SENTINAL))
          resistance += (1/r1); 
       if (resistance == 0)
          printf("\nNo values entered.\n\n");
          printf("\nTotal resistance of the ciruit is %.3f ohms\n", 1/resistance);
       return 0;
    i have tried

    if ((r1 == SENTINAL) || (r1 == EOF))
    i have also tried saying

    if (scanf("%lf", &r1) == EOF)
    the scanf code ends the input but it changes my numbers...can anyone give me any insight to what i am doing wrong? thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    rl can never have the value EOF. scanf will return the value EOF. Since you need to check for both EOF and zero, then do so:
    if (retval == EOF || inputval == 0)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I think you should write your loop like this instead:
    while (scanf("%lf", &r1) == 1 && r1 != SENTINAL) {
        resistance += 1.0 / r1;
    Incidentally, I think that SENTINAL should be SENTINEL, and you really should indent your code more consistently.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    thank you!

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