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.
#include <fstream>
#include <iomanip>
#include <iostream>
usingnamespace std;
int main()
{
int number;
//int number = 0;
int total_sum = 0;
int high = 0;
int low = 0;
double average;
doubleconst 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;
}
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.