Well, I dont like the way your program is structured for a start - looking at
if (!myfile.is_open()) {
cout << "Error: The file does not exist \n" << endl;
cout << " ";
}
else if (myfile.is_open())
{
ifstream x;
numIntElement(x);
}
else if (myfile.is_open())
{
}
what is the last 'else if' clause supposed to do ?
you could do (for example)
std::ifstream myfile(filename);
if(!myfile) {
cout << endl << "Failed to open file, check that it exists and you have access " << filename << endl;
return 1;
}
which avoids the if then else type stuff you have and just exits the program with a message if the file doesnt exist - you could wrap either of the tests Ive shown for file exists into a separate function if you didnt want to exit the program but do something else instead - nb: there are plenty of other ways to check if a file exists, Im assuming you have a 'local' rather than 'network' drive/path to your file
...... for 'how many' integers are in the file, you already have a while loop - if I just write a snippet of my own :-
int n = 0;
while(!myfile.eof()) {
myfile >> n;
}
That reads integers into n while not at the end of file - so, one way of getting the number of integers in the file might be to add a count to the loop, something like this :-
int n = 0;
int countIntsInFile = 0;
while (!myfile.eof()) {
myfile >> n;
countIntsInFile += 1;
}
does that help you - I'd suggest you think a bit more about how you put this together, which depends on why you're
a) getting the number of integers in the file
b) what else you'll be doing with the file/integers etc
hth