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;
	}

Start your high and low with the first value read. Then compare each consecutive value with the high and low and change accordingly.

your while loop isn't working like you think it is i believe. lines 50-61 should be in the while loop and you have them outside it.

I am still very confused over this.....

would I put something like the following in lines 51 - 61?

while (infile, ID, Sales)
{
    if ( hsales < Sales )
        hsales = Sales;
    hcount++;

    if ( lsales > Sales )
        lsales = Sales;
    lcount++;
{

    displaysales(outfile, hsales, lsales);
}

I know you guys do not like doing assignments for us, but I have done most of it wink, wink

Do you want to restart hsales and lsales every time through the loop? Is that what I recommended?

Start your high and low with the first value read. Then compare each consecutive value with the high and low and change accordingly.

Trouble is, I do not know what the first value will be, I am just presented with 3 different files and the first value is different each time

Trouble is, I do not know what the first value will be, I am just presented with 3 different files and the first value is different each time

You know when you read it.

Ok, so how do I declare the hsales and lsales to be = the first input? I have tried hsales = Sales and gets me nothing...I think most of my problem is in that declaration, but for the life of me I can not figure out how to get it to work. Not too mention where to declare these....

Read the first entry of the file to get Sales. Load hsales and lsales. It's ain't rocket science.

Forgive me for being a c++noob, but it isnt rocket science when you are just starting out. I was hoping for an example.

anyone? How do you load in values to a variable?

anyone? How do you load in values to a variable?

If you add the following cout << there

while( getInput(infile, ID, Sales) )
{
  cout << "ID:\t" << ID << "\tSales:\t" << Sales << endl;
  displayResults(outfile, ID, Sales);
}

What is the output on your screen?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.