mercredi 24 juin 2020

Finding Min and Max numbers in a txt file

see please the picture I have problem in finding the min and max number in txt file. The number are stored in a file as one number per line. The Programm should go through these numbers and find the biggest and smallest number.

#include<fstream>
#include<cstdlib>
#include<iostream>
using namespace std;

int main()
{
int counter=0;
float number, sum=0, average, Max = 0, Min = 0;
char file_name [20];
cout << "enter filename: ";
cin >> file_name;
ifstream input;;
input.open(file_name);
if (! input)
{
cout << "Can't open file" << file_name;
//exit (0);
}

while (input>>number)
{
    counter++;
    sum=sum+number;
}
average=sum/counter;

while (input>>number)
{
    if (number>Max)
    number=Max;
    else
    number=Min;
}


cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout<<"The average file in file test is was "<<average<<endl;
cout<<"The largest number is: "<<Max<<endl;
cout<<"The smallest number is: "<<Min<<endl;


input.close();

return 0;
}

When i run it, the min and max values are zero! Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire