Hello all, I have a project in which I need to open an external file which contains to columns of numbers ie:
1000 1000
1111 500
1500 1000
2000 900
2222 2000
2500 2000
3000 1500
3333 2000
3500 2000
4000 600
4444 2500
4500 2000
5000 2500
5500 2222
5555 2000
6000 1999
6500 2222
The first column is an ID and the second is an amount.
I am to create an output which lists the data and gives the highest and lowest amounts...then counts the occurrences of the high and low. I have most of the program written, however cannot figure out how to determine the high and low.....any help would be appreciated
//program to read data from external file and output the contents with the
//highest commision and occurance
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
istream& getInput(istream& input, int &ID, double &Sales);
void displayHeading(ostream& outfile);
void displayResults(ostream& outfile, int ID, double Sales);
void displaysales(ostream& outfile, double hsales, double lsales);
int main()
{
int ID;
double Sales;
double hsales;
double lsales;
ifstream infile ("sales1.txt");
if (!infile)
{
cerr << "Error: cannot open file\n";
system ("pause");
return 1;
}
infile >> ID >> Sales;
if (!infile)
{
cout <<"Error: unable to input data from file\n";
return 2;
}
ofstream outfile ("PJ522Out_1_Nealey.txt");
if (!outfile)
{
cerr <<"Error: cannot open PJ522_1_Nealey.txt";
system ("pause");
return 2;
}
//Format the output file
outfile <<fixed <<showpoint << setprecision(2);
displayHeading( outfile );
while( getInput(infile, ID, Sales) )
{
displayResults(outfile, ID, Sales);
}
hsales = Sales;
lsales = Sales;
{
if (Sales > hsales) hsales = Sales;
if (Sales < lsales) lsales = Sales;
}
{
displaysales(outfile, hsales, lsales);
}
return 0;
}
istream& getInput(istream& input, int &ID, double &Sales)
{
input >> ID >> Sales;
return input;
}
void displayHeading(ostream& outfile)
{
outfile <<setw(15)<<" ACME Sales Report"<<endl;
outfile <<endl;
outfile <<setw(5)<<"ID"<<setw(10)<<"Sales"<<endl;
}
void displayResults(ostream& outfile, int ID, double Sales)
{
outfile <<setw(6)<<ID<<setw(10)<<Sales<<endl;
}
void displaysales(ostream& outfile, double hsales, double lsales)
{
outfile <<hsales<<" "<<lsales<<endl;
}