help with EOF

This is a discussion on help with EOF within the C Programming forums, part of the General Programming Boards category; hello, i am working on this program that calculates the resistance of an arbitrary number of floating points, the input ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    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

    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))
             break; 
          resistance += (1/r1); 
    }
       
       if (resistance == 0)
          printf("\nNo values entered.\n\n");
       else 
          printf("\nTotal resistance of the ciruit is %.3f ohms\n", 1/resistance);
          
       system("pause");
       return 0;
    }
    i have tried

    Code:
    if ((r1 == SENTINAL) || (r1 == EOF))
             break;
    i have also tried saying

    Code:
    if (scanf("%lf", &r1) == EOF)
    break;
    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
    Posts
    14,185
    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:
    Code:
    if (retval == EOF || inputval == 0)

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    I think you should write your loop like this instead:
    Code:
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    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, 08: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21