#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void maximum(int Inumber, int& OMax)
{
if (OMax < Inumber)
OMax = Inumber ;
}
void minimum(int Inumber, int& OMin)
{
if (Inumber < OMin)
OMin = Inumber;
}
void ave(int Inumber, double& Oaverage)
{
Oaverage = Oaverage + Inumber;
}
int main()
{
ifstream inputFile;
inputFile.open("HiFile.txt", ios_base::in);
if (inputFile.is_open())
{
double average = 0;
int number;
int count=1;
inputFile >> number;
int Max = number, Min = number;
while (inputFile >> number)
{
count++;
}
int Iarray[10]={0};
for (int arrayIndex = 0; arrayIndex < count; arrayIndex++)
{
inputFile >> Iarray[arrayIndex]; //I think the problem is here
}
for (int arrayIndex = 0; arrayIndex < count; arrayIndex++)
{
maximum(Iarray[arrayIndex], Max);
minimum(Iarray[arrayIndex], Min);
ave(Iarray[arrayIndex], average);
}
average = average / count;
cout << "Finished processing the file" << endl;
cout << "Minimum value: "<< Min << endl;
cout << "Maximum value: "<< Max << endl;
cout << setprecision(2);
cout << "Average value: "<< average << endl;
for (int arrayIndex = 0; arrayIndex < count; arrayIndex++)
cout << Iarray[arrayIndex]<< endl;
}
else
cout << "Failed to open the file." << endl;
// close both streams
inputFile.close();
return 0;
}
I cannot read the file to array. The program is to read a file with at most 10 integer, than put it in an array and find max, min, average of it. I struggle at how to put the data from the file to an array.

New Topic/Question
Reply



MultiQuote



|