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