In the display function when i output it shows multiple output for some agents, but i only want it to show the final one, how can i do this?

#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<cmath>
#include<string>
// Associating program identifiers with external file names
#define in_file "data.txt"//input file name
#define out_file "result.txt"//output file name
using namespace std;
void error();
void display();
void total();
void SWITCH1(int&, int&);
void SWITCH2(float&, float&);


ifstream ins;// associates ins as an input stream
ofstream outs;// associates outs as an output stream
const int size=19;
int agent[size];
int sales[size];
float value[size];
float amount[size];
int count=0;


int main()
{
	outs.open(out_file);
	error();
	display();
	total();
	

	getch();
	outs.close();
}

void error()
{
	ins.open(in_file);
	cout<<"LIST OF INVALID ENTRIES"<<endl;
	int i=0;
	while(!ins.eof())
	{	
		ins>>agent[i]>>sales[i]>>value[i];
		count++;
		cout<<fixed<<showpoint;
		cout<<setprecision(2);
		if((agent[i]<1)||(agent[i]>20))
		{
			cout<<setw(2)<<agent[i]<<"   "<<setw(2)<<sales[i]<<"   "<<setw(7)<<value[i];
			cout<<"        **invalid agent #**"<<endl;
		}
		else if(value[i]<0)
		{
			cout<<setw(2)<<agent[i]<<"   "<<setw(2)<<sales[i]<<"   "<<setw(7)<<value[i];
			cout<<"        **invalid price**"<<endl;
		}
			i++;
	}
	cout<<endl;
	ins.clear();
	ins.close();
}



void display()
{
	ins.open(in_file);
	int total_agent[size];
	//int total_amount[size];
	cout<<"                 TOTAL SALES BY AGENT"<<endl;
	cout<<"AGENT NUMBER     #SALES     VALUE/SALE     AMOUNT"<<endl;
	int i=0;
	int t=0;
	cout<<fixed<<showpoint;
	cout<<setprecision(2);
	while(!ins.eof())
	{	
		ins>>agent[i]>>sales[i]>>value[i];
		amount[i]=sales[i]*value[i];
		i++;
	}

	for(int n=count-2; n>=0; n--)
	{
		for(int m=0; m<=n; m++)
		{
			if(agent[n]==agent[m])
			{
				amount[n]+=amount[m];
				sales[n]+=sales[m];
			}
		}
		amount[n]=amount[n]/2;
		sales[n]=sales[n]/2;
		value[n]=amount[n]/sales[n];

	}
		

	for(int a=count-2; a>=0; a--)
			for(int b=0; b<=a; b++)
			{
				if(agent[b]>agent[b+1])
				{
					SWITCH1(agent[b], agent[b+1]);
					SWITCH1(sales[b], sales[b+1]);
					SWITCH2(value[b], value[b+1]);
					SWITCH2(amount[b], amount[b+1]);
				}
			}
	for(int t=0; t<=count; t++)
	{
			cout<<setw(2)<<agent[t]<<"              "<<setw(2)<<sales[t]<<"          "<<setw(7)<<value[t]<<"        "<<setw(7)<<amount[t]<<endl;
	}
	cout<<endl;
	ins.clear();
	ins.close();
}
void SWITCH1(int& x, int& y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
}
void SWITCH2(float& x, float& y)
{
	float temp;
	temp=x;
	x=y;
	y=temp;
}


void total()
{
	ins.open(in_file);
	int i=0;
	int total_sales=0;
	float average_sales;
	float total_volume=0;
	while(!ins.eof())
	{	
		ins>>agent[i]>>sales[i]>>value[i];
		amount[i]=sales[i]*value[i];
		cout<<fixed<<showpoint;
		cout<<setprecision(2);
		total_sales+=sales[i];
		if(amount[i]>0)
			total_volume+=amount[i];
		average_sales=total_volume/total_sales;
		

		i++;
	}
	cout<<"TOTAL SALES             "<<total_sales<<endl;
	cout<<"AVERAGE SALES VALUE     "<<average_sales<<endl;
	cout<<"TOTAL VOLUME IN SALES   "<<total_volume<<endl;
	cout<<endl;
	
	ins.clear();
	ins.close();
}

this is my output: http://img4.imageshack.us/i/travelagentoutput3.png/
as you can see, for example, agent one displays something twice, but i only need it to display the second one, the one with 5 sales, $270 value, and $1350 amount

One way to do it is to check the agent array to see if it already contains an entry for the agent just read in from the file. If it already exists then just sum the sales and value into the respective sales and values column. something like this:

int FindAgent(int agnt, int maxrow)
{
   for(int i = 0; i < maxrow; i++)
      if( agent[i] == agnt)
         return i;
   return -1;
}

void display()
{
ins.open(in_file);
int total_agent[size];
//int total_amount[size];
cout<<"                 TOTAL SALES BY AGENT"<<endl;
cout<<"AGENT NUMBER     #SALES     VALUE/SALE     AMOUNT"<<endl;
int i=0;
int t=0;
cout<<fixed<<showpoint;
cout<<setprecision(2);
while(!ins.eof())
{
    int agnt, sals;
    float val;
    ins >> agnt >> sals >> val;
    int row = FindAgent(agnt,i);
    if( row < 0)
    {
       agent[i] = agnt;
       sales[i] = sals;
       value[i] = val;
       amount[i]=sales[i]*value[i];
       i++;
    }
    else
    {
       sales[row] += sals;
       value[row] += val;
       amount[row]=sales[row]*value[row];
    }
}

these are the changes i made and all the calculations work great, thanks Ancient Dragon, i've been at that one for atleast 2 days now so that was super helpful, only thing i have a question is about the output
this is what i get: http://img834.imageshack.us/i/travelagentoutput4.png/
as u see for agent 13 and 18 there are two outputs, but should only be one, i could use an if statement like:

if(!(value[t]>0)&&(amount[t]=0))

but i was wondering if there was a different way?
these are the changes i made to my program:

void display()
{
	ins.open(in_file);
	int total_agent[size];
	//int total_amount[size];
	cout<<"                 TOTAL SALES BY AGENT"<<endl;
	cout<<"AGENT NUMBER     #SALES     VALUE/SALE     AMOUNT"<<endl;
	int i=0;
	int t=0;
	int j=0;
	int sum=0;
	cout<<fixed<<showpoint;
	cout<<setprecision(2);
	while(!ins.eof())
	{
		int agnt, sals;
		float val;
		ins>>agnt>>sals>>val;
		int row = FindAgent(agnt,i);
		if( row < 0)
		{
		   agent[i]=agnt;
		   sales[i]=sals;
		   value[i]=val;
		   amount[i]=sales[i]*value[i];
		i++;
		}
		else
		{
		   sales[row]+=sals;
		   sum=sals*val;
		   amount[row]+=sum;
		   value[row]=amount[row]/sales[row];
		}
	}
	for(int a=count-2; a>=0; a--)
			for(int b=0; b<=a; b++)
			{
				if(agent[b]>agent[b+1])
				{
					SWITCH1(agent[b], agent[b+1]);
					SWITCH1(sales[b], sales[b+1]);
					SWITCH2(value[b], value[b+1]);
					SWITCH2(amount[b], amount[b+1]);
				}
			}
	for(int t=0; t<=count; t++)
	{
		if((agent[t]>0)&&(value[t]>-1))
			cout<<setw(2)<<agent[t]<<"              "<<setw(2)<<sales[t]<<"          "<<setw(7)<<value[t]<<"        "<<setw(7)<<amount[t]<<endl;
	}
	cout<<endl;
	ins.clear();
	ins.close();
}

also i made another function which is supposed to list the agents that are not on the output list, from 1 to 20
this is what i have so far:

void not_part()
{
	ins.open(in_file);
	int i=0;
	int t=0;
	int j=0;
	int sum=0;
	cout<<"AGENTS WHO DID NOT PARTICIPATE IN THE CAMPAIGN"<<endl;
	cout<<fixed<<showpoint;
	cout<<setprecision(2);
	while(!ins.eof())
	{
		int agnt, sals;
		float val;
		ins>>agnt>>sals>>val;
		int row = FindAgent(agnt,i);
		if( row < 0)
		{
		   agent[i]=agnt;
		   sales[i]=sals;
		   value[i]=val;
		   amount[i]=sales[i]*value[i];
		i++;
		}
		else
		{
		   sales[row]+=sals;
		   sum=sals*val;
		   amount[row]+=sum;
		   value[row]=amount[row]/sales[row];
		}
	}
	for(int t=0; t<=count; t++)
		for(int h=1; h<=20; h++)
		{
		if((h!=agent[t])||((value[t]=0)&&(amount[t]=0)))
			cout<<setw(2)<<agent[t]<<endl;
		}

	cout<<endl;
	ins.clear();
	ins.close();
}

int FindAgent(int agnt, int maxrow)
{
   for(int i = 0; i < maxrow; i++)
      if( agent[i] == agnt)
         return i;
   return -1;
}

i know for sure the problem is in this statment:

for(int t=0; t<=count; t++)
		for(int h=1; h<=20; h++)
		{
		if((h!=agent[t])||((value[t]=0)&&(amount[t]=0)))
			cout<<setw(2)<<agent[t]<<endl;
		}

im just wondering how i can change it around so that it displays the number once if its not there rather than multiple times after checking through each number, cause what i have check its over and over again, which is wrong

This article has been dead for over six months. Start a new discussion instead.