Hello again all, I have a project in which I need to read in a text file:
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
this is one of three files....from this I am to determine the highest and lowest amounts and count the occurances...I have most of the program completed, however I cannot figure out the part to determine the highest, lowest..etc. Below is the portion of the program which I think should give me the output I desire.
while (infile, ID, Sales)
{
if ( hsales < Sales )
hsales = Sales;
hcount++;
if ( lsales > Sales )
lsales = Sales;
lcount++;
{
however all I get is:
ACME Sales Report
ID Sales
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
6666 2500
7000 100
7500 2100
7777 2150
2150 0
(it is not fully formatted, I will clean that up when I get the output correct)
full code is as follows
//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, int &Sales);
void displayHeading(ostream& outfile);
void displayResults(ostream& outfile, int ID, int Sales);
void displaysales(ostream& outfile, int hsales, int lsales);
int main()
{
int ID;
int Sales;
int hsales = 0;
int lsales = 0;
int hcount = 0;
int lcount = 0;
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);
}
while (infile, ID, Sales)
{
if ( hsales < Sales )
hsales = Sales;
hcount++;
if ( lsales > Sales )
lsales = Sales;
lcount++;
{
displaysales(outfile, hsales, lsales);
}
return 0;
}
}
istream& getInput(istream& input, int &ID, int &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, int Sales)
{
outfile <<setw(6)<<ID<<setw(10)<<Sales<<endl;
}
void displaysales(ostream& outfile, int hsales, int lsales)
{
outfile <<hsales<<" "<<lsales<<endl;
}