Thread: eof member function on the stream cin

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    eof member function on the stream cin

    How would I alter this program in order to use the eof member function on the stream cin??

    Code:
    int main(void)
    {
      int i, sum = 0;
    
      cout << "Enter an int: " ;
      while ( cin >> i ) {
        cout << "  i=" << i << endl;
        sum += i;
        cout << "Enter an int: " ;
      }
      cout << "The sum of the numbers entered is: " << sum << endl;
      return 0;
    }
    This program will terminate as soon as anything but an integer is entered.

    I've tried many different things, but when I use eof, the program doesn't recognize that only integers have to be entered, i.e. it takes in characters as well. I keep getting either an infinite loop or the program never terminates.

    I also have a question about the fail() member function. If I have
    Code:
    if (!infile.fail())
    is there anyway that instead of calling the fail() member function on the stream, to put the "not" operator in front of the stream name? I've tried this as well, but I can't understand how to use the "not" operator with the stream name in order to make sure that the file is indeed opened.

    Any help would be appreciated as always.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    This might work. I'm not sure how to get it to recognize eof with my borland compiler though. I tried <ctrl>d and <ctrl>c.
    Code:
    int main(void)
    {
      int i, sum = 0;
    
      cout << "Enter an int: " ;
      cin >> i;
      while ( !cin.eof()) {
        if (cin.fail())
        {
          cin.clear();
          cin.ignore();
        }
        else
        {
          cout << "  i=" << i << endl;
          sum += i;
        }
        cout << "Enter an int: " ;
        cin >> i;
      }
      cout << "The sum of the numbers entered is: " << sum << endl;
      return 0;
    }
    To answer your second question, you can use something like:
    Code:
    ifstream in(filename);
    if (!in.is_open())
    {
       cout << "File not found:" << filename << endl;
       return 1;
    }
    Or:
    Code:
    ifstream in(filename);
    if (!in)
    {
       cout << "File not found:" << filename << endl;
       return 1;
    }
    Last edited by swoopy; 10-29-2002 at 01:38 AM.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    81

    One more thanks!

    Once again I want to thank all those who responded as well as those that took to the time to read my post. I'm learning more here than in class or in any books I read!!!

    I'm sorry if I'm posting twice only to thank people, hope it doesn't offend anyone, but I just can't leave posts that were answered without answering back with a thank you!!!
    "Our greatest glory consists not in never failing,
    but in rising every time we fall."

    Oliver Goldsmith (1730-1774).
    Anglo-Irish writer, poet and playwright.

  4. #4
    THat is what we're here for. And, i can almost promise, even the super-mods and webmasters even learn in here. (They can verify that)

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. member member function pointers
    By Bigbio2002 in forum C++ Programming
    Replies: 3
    Last Post: 12-04-2005, 05:14 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM