How to get min. and max from a txt file?

I'm writing a program that gets the minimum, maximum, and average from a txt file. The issue is I'm stuck on how to get the minimum and maximum from the txt file. Can you guys help by giving me an insight on how to go about this? Thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  
#include <iostream>
#include <fstream>
using namespace std;

void ProcessTest(){
        double highest = 0;
        double lowest = 0;
        double average;
        int sum = 0;
        int x;
        char fileName[50];
        ifstream dataFile;  //an input file stream object
        cout << "Enter file name you want to open: ";
        cin.getline(fileName, 50);
        dataFile.open(fileName);

    //open the data file
    // datafile.open("lab6-3.dat");
    // Note: There is 31 numbers in this list
    if(!dataFile.is_open()){
        cout << "Error occurred when opening file!";
    }

    while(dataFile >> x){
       average = (sum += x) / 31;
        cout << x << "\n";
    }
    cout << x << "\n";
    cout << "The average is: " << average << endl;
    // Don't know how to get the highest and lowest from txt file...
    cout << "The lowest is: " << lowest << endl;
    cout << "The highest is: " << highest << endl;


    dataFile.close();
}

int main()
{

    ProcessTest();

    return 0;
}



Last edited on
Perhaps you need to increase your compiler warning level? Here is what my compiler tells me about your code:

1
2
3
4
5
main.cpp||In function ‘void ProcessTest()’:|
main.cpp|6|warning: no previous declaration forvoid ProcessTest()’ [-Wmissing-declarations]|
main.cpp|10|warning: unused variable ‘current’ [-Wunused-variable]|
main.cpp|31|warning: ‘lowest’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
main.cpp|32|warning: ‘highest’ may be used uninitialized in this function [-Wmaybe-uninitialized]|
I changed the highest and lowest to = 0;
I also spotted the flaw of having current there and doing nothing with it so I took that out.
Now, is the "main" issue which is how to get the minimum and maximum from a txt file.
I was thinking of using a while loop but the problem is I'm having trouble with wrapping my mind around how I can take all the numbers and (somehow) find the min and max.
Any ideas?
How would you get the highest and lowest if you were working with a series of flash cards?

Next look at this snippet:
1
2
3
4
5
6
7
8
    if(!dataFile.is_open()){
        cout << "Error occurred when opening file!";
    }

    while(dataFile >> x){
       average = (sum += x) / 31;
        cout << x << "\n";
    }


Wouldn't it be better to compute the average after you have computed the sum and counted the number of elements? If you count the number of elements you can use this code for files with an unknown number of elements. Also be careful, you are doing integer math in your calculation so you won't have any fractions. This would probably be a good place to locate the minimum and maximum as well.

Lastly if the file doesn't open properly wouldn't it be better to inform the user and skip trying to read the file?
Last edited on
Topic archived. No new replies allowed.