Min Not Working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
double sum = 0;
		double max = 0;
		double min = 0;
		do
		{
			myFile >> x;
			sum += x;
			if (!myFile.eof())
			{
				cout << x << endl;
				count ++;
				if(count%20==0)
				{ 
					double average = sum/count;
					cout << "The Average is: " << average << endl;
					cout << "The min is: " << min << endl;
					cout << "The max is: " << max << endl;
					system("pause");
				}
			}
		} while (!myFile.eof());


This is my code and because the min is set to 0, no matter the number (unless it is negative) the min will be 0 even if it is 4 in the set of numbers. How do I get the min to be the lowest number in the set of numbers from a file?
It's not that min "isn't working", you just haven't done anything to make it work yet.
Sorry, I forgot I had taken it out, here is what I have to get the min and max
1
2
max = (x > max) ?x:max;
min = (x < min) ?x:min;

It goes right below the sum+=x
Last edited on
Try moving the declarations of min and max under sum += x

and write
1
2
static double min = x;
static double max = x;


there instead. Not very elegant, but without completely changing the way you are doing this - which I would actually recommend - you won't get around this.
Awesome, that worked like a charm.
Topic archived. No new replies allowed.