As everyone stated, use std::cin directly - you do not need to open the input file, your shell has already done this for you.
But please, please, please, do not use cin.eof() to check if you have reached the end of your input. If your input is corrupted, your program freezes. Even if your input is not corrupted, your program may (but not necessarily) start a loop in extra time.
Try using this loop:
int a[SIZE]; int i = 0; while( std::cin >> a[i]) { ++i; }
Or add confidence using std::vector , which will automatically grow:
std::vector<int> a; int i; while(std::cin >> i) { a.push_back(i); }
Or use general algorithms:
#include <iterator> #include <algorithm> ... std::vector<int> a; std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<int>(), std::back_inserter(a));
source share