I'm taking a beginner level C++ class, and have gotten stuck on one of the practice problems.
The assignment is to read in a file with a random set of integers and to print out the sum of the integers, the average, the number of even integers and the number of odd integers.
I have gotten as far as reading in the file, and I know I need to do some form of for loop or while loop to use the integers that are read in to compute the rest, but I'm not sure how to do that without knowing the exact number of integers or the range of the integers that will be in the files (We have 8 test files that range from 3 integers to 50 integers).
If someone could lead me in the right direction on how to write a loop to compute the sum of the integers without knowing how many intergers I will have, I believe I can figure out the rest.
This is the code I have so far that will read in the integers in the file and print them in the terminal.
int main ()
{
int sum = 0.0; // sum of integers
double average = 0.0; // average of integers
int even = 0.0; // number of even integers
int odd =0.0; // number of odd integers
ifstream fin;
string file_name;
int x;
cout << "Enter the file name: ";
cin >> file_name;
fin.open(file_name.c_str(), ios::in);
if (!fin.is_open())
{
cerr << "Unable to open the file." << file_name << endl;
exit(10);