How to correctly reset a counter to zero

Hi Guys, on the below code I'm having issues resetting low counter to 0.
If I set the low counter to =0 it does not work it give me a value of zero, but I saw on a different forum where they reset it using numbers for example low=999999 and that works on my code ,but I would like to know whats the reason behind that, and why low=0 does not reset it. The counter I'm referring too is in line 71. Thank's guys in advance.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{

	int number;
	//int number = 0;
	int  total_sum = 0;
	int high = 0;
	int low = 0;
	double average;
	double const numbers_Per_record = 7;



	std::ifstream infile("I:\\infile.txt");
	if (!infile)
	{
		std::cout << "Cannot open input file. Program terminates!\n";
		return 1;
	}
	//Open output.txt to append to it 
	ofstream outfile("I:\\lab5a_output.txt");
	outfile << left;




	do {
		cout << "Calculate , Total, highest, lowest,Average for numbers " << ":";
		outfile << "The dataset for input line #" << " is: ";
		//makes sure seven numbers go at a time from the file 
		for (int seven_numbers = 1; seven_numbers <= 7; seven_numbers++)
		{

			infile >> number;
			cout << number << " ";
			outfile << number << " ";
			total_sum = total_sum + number;
						
			if (number > high)
			{
				high = number;
			}
			if (number < low)
			{
				low = number;
			}

		}
		average = (total_sum / numbers_Per_record);
		cout << endl;
		outfile << endl;
		outfile << "total=" << total_sum << endl;
		cout << "total=" << total_sum << endl;
		cout << "highest=" << high << endl;
		outfile << "highest=" << high << endl;
		cout << "lowest=" << low << endl;
		outfile << "lowest=" << low << endl;
		cout << setprecision(4) << "Average=" << average << fixed << endl;
		outfile << setprecision(4) << "Average=" << average << fixed << endl;


		
		total_sum = 0;
		high = 0;
		low = 0; //works if i set it to 9999999 but I dont undestand why ?

		// infile.eof helps read the records to the end of the page 
	} while (!infile.eof());


	return 0;
}
Last edited on
You have high and low as the same value.
This is what your if statements says :

let's say number = 12;
int high = 0;
int low = 0;

1st loop
if (number > high) //true if 12 is greater than 0; (true)
{
high = number; // high = 12
}
if (number < low) //true if 12 is less than 0; (false) 12 is not less than 0.
{
low = number; // low = 0
}
The only value that will be less than 0 is a negative number. It works with 99999 because all the input value are less than 99999.
Topic archived. No new replies allowed.