Eof

This is a discussion on Eof within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { int array[10]; int i = 0; int count = 0; cout ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Eof

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int array[10];
    
    	int i = 0;
    	int count = 0;
    
    	cout << "Enter up to ten numbers" << endl;
    
        while (cin >> array[i++])
    	{
    		count++;
    
    		if (cin.eof())
    			break;
    
    		if (i == 10)
    			break;
    	}
    
    	cout <<"\nYour numbers are..." << endl;
    
    	for (i = 0; i < count; i++)
    		cout << array[i] << endl;
    
    	return 0;
    
    }
    Why doesn't the code break when EOF is entered?

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    EOF only works for files (disk files). Some EOF info was just added to the programming FAQ last week.

  3. #3
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Why doesn't the code break when EOF is entered?
    Because the while loop tests for eof, the if statement is never reached so you can't break. But it works fine since the loop exits when cin returns a failed state. You might have to hit ctrl+d or ctrl+z or whatever signals EOF for you more than once if you're running from an IDE. I know that Visual C++ 6.0 makes you hit it twice before it works like you want. Based on that, you could just do this and it would still work fine
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int array[10];
        int i = 0;
        int count = 0;
    
        cout << "Enter up to ten numbers" << endl;
    
        while (cin >> array[i++] && i < 10)
            count++;
    
        cout <<"\nYour numbers are..." << endl;
        for (i = 0; i < count; i++)
            cout << array[i] << endl;
    
        return 0;
    }
    EOF only works for files (disk files).
    Nope, it works fine for interactive input, you just have to signal it manually. Like ctrl+z for Windows. Of course, it could be said that everything is a stream to C++, even files and interactive input, so we're both right.

  4. #4
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Brighteyes

    I know that Visual C++ 6.0 makes you hit it twice before it works like you want.
    Is there a way to make the Visual C++ compiler do what you want it do with one eof declaration?

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Is there a way to make the Visual C++ compiler do what you want it do with one eof declaration?
    Nope, I've tried it for some time with no success in either C or C++. The workaround I use if I really need one eof to be one eof is to compile the program with Borland C++. Visual C++ 6.0 is one of those implementations where cin itself AND the lower level functions that it calls require a new eof before reporting it back, so you have to enter it multiple times.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Brighteyes

    Nope, I've tried it for some time with no success in either C or C++.
    Not even in C?

    Well this code worked for me:

    Code:
    #include <stdio.h>
    
    int main()
    {
    	int array[10];
    
    	int count = 0;
    	int i = 0;
    
    	printf("Enter up to ten numbers\n");
    
    	while (scanf("%d", &array[i++]) != EOF && i < 10)
    	{
    		count++;
    	}
    
    	printf("\n");
    
    	for (i = 0; i < count; i++)
    
    	printf("%d\n", array[i]);
    
    	return 0;
    }
    I never have to use that double eof thing in C when I'm using Visual C++ 6.0.

  7. #7
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    I never have to use that double eof thing in C when I'm using Visual C++ 6.0.
    Okay, I lied. When reading numbers with scanf you tend not to have the same end of file problems as with string input. This requires two eof's
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int rc;
        char buf[1001];
    
        rc = scanf("%1000[^\n]", buf);
        if (rc == EOF)
            return 1;
        else if (rc < 1)
            printf("Boop!\n");
        else
            getchar();
    
        printf("Beep!\n");
    
        return 0;
    }

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, 03:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 04: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, 06:28 AM

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