You are the manager of a travel agency with 20 employees. You have announced a promotional
campaign for your agents to sell vacation packages. Each day you will enter the ID number of the agent (#1-#20), the number of packages sold, and the dollar value of the package. Entries will be in a random order and the total $ value is accumulated for a 3 week period. At the end of the campaign you want a print out of those agents who sold less than $1000.00 in the 3 week time frame. You will also want a list of those that did not participate in the campaign.

That's the assignment and heres what I have so far, the problem is that in the void display function doesn't display anything, once I can figure this out then I can move on to the other steps.

#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<cmath>
// 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 errors(int [], int [], float []);
void display(int [], int [], float [], float []);

ifstream ins;// associates ins as an input stream
ofstream outs;// associates outs as an output stream
const int r=19;

int main()
{
	//const int r=19;
	int a[r];
	int b[r];
	float c[r];
	float w[r];
	ins.open(in_file);// associates ins as an input stream
	outs.open(out_file);// associates outs as an output stream

	//read_values(a, b, c);
	//amount(a, b, c);

	int i=0;
	//float w[r];
	char next_char;
	

	cout<<"Errors: "<<endl;
	while(!ins.eof())
	{
		ins>>a[i]>>b[i]>>c[i];
		errors(a, b, c);	
	}
	while(!ins.eof())
	{
		ins>>a[i]>>b[i]>>c[i];
		w[i]=b[i]*c[i];
		display(a, b, c, w);
	}


	_getch();

	ins.close();// closing input file
	outs.close();// closing output file
}
void display(int f[], int g[], float h[], float d[])
{
	int i=0;
	
		cout<<fixed<<showpoint;
		cout<<setprecision(2);

		cout<<setw(2)<<f[i]<<" "<<setw(2)<<g[i]<<" "<<setw(7)<<h[i]<<"   "<<setw(7)<<d[i]<<endl;

				cout<<endl;
				i++;

}

void errors(int x[], int y[], float z[])
{
	int i=0;
	
		cout<<fixed<<showpoint;
		cout<<setprecision(2);
		if((x[i]<1)||(x[i]>20))
		{
			cout<<setw(2)<<x[i]<<" "<<setw(2)<<y[i]<<" "<<setw(7)<<z[i]<<" **invalid agent #**" <<endl;
		}
		else if(z[i]<0)
		{
			cout<<setw(2)<<x[i]<<" "<<setw(2)<<y[i]<<" "<<setw(7)<<z[i]<<" **invalid price**" <<endl;
		}

		i++;
	
}

Recommended Answers

All 19 Replies

You need a loop. The way your function is situated now, you call it by passing in the array without knowing how many members it has,which doesn't actually matter because you're printing out the first element, incrementing i and then returning. "i" is not maintained between calls, so you're starting at zero each time.

But there's another problem, when you're reading in your data points in main, "i",(which is not connected to the "i" in the display function in any way) is not changing at all, so you're writing each subsequent read from on top of the first elements of each array over and over.

ok so now i have this

#include<iostream>
#include<fstream>
#include<conio.h>
#include<iomanip>
#include<cmath>
// 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();

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 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];
		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 temp;
	cout<<"                 TOTAL SALES BY AGENT"<<endl;
	cout<<"AGENT NUMBER     #SALES     VALUE/SALE     AMOUNT"<<endl;
	int i=0;
	while(!ins.eof())
	{	
		ins>>agent[i]>>sales[i]>>value[i];
		amount[i]=sales[i]*value[i];
		cout<<fixed<<showpoint;
		cout<<setprecision(2);
		if((agent[i]>0)&&(agent[i]<21))
		{
		for(int a=13-2; a>=0; a--)
			for(int b=0; b<=a; b++)
			{
				if(agent[b]>agent[b+1])
				{
					temp=agent[b];
					agent[b]=agent[b+1];
					agent[b+1]=temp;
				}
			}
		cout<<setw(2)<<agent[i]<<"              "<<setw(2)<<sales[i]<<"          "<<setw(7)<<value[i]<<"        "<<setw(7)<<amount[i]<<endl;
		}
		i++;
	}
	cout<<endl;
	ins.clear();
	ins.close();
}

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

the problem is that in the void display() function once i sort the agent numbers, how do i sort the rest so it changes with the agent, because the agents sales and amount of sales should change accordingly

my input is:
1 3 250.00
2 0 0
15 1 1000.00
3 4 300.00
12 2 500.00
1 2 300.00
3 4 115.00
21 3 400.00
-1 4 250.00
15 1 200.00
9 5 -150.00
18 2 140.00
13 2 550.00

On line 80+, when you're swapping the agent array, swap the other arrays at the same time. If you're swapping elements 8 and 9 of agent[], swap 8 and 9 of sales also. "i" will give you the index either way. To declutter the code in that region, see if you can come up with a swap function.

i made these changes from lines 80+

for(int a=13-2; a>=0; a--)
			for(int b=0; b<=a; b++)
			{
				if(agent[b]>agent[b+1])
				{
					temp1=agent[b];
					agent[b]=agent[b+1];
					agent[b+1]=temp1;
					
					temp2=sales[b];
					sales[b]=sales[b+1];
					sales[b+1]=temp2;

					temp3=value[b];
					value[b]=value[b+1];
					value[b+1]=temp3;

					temp4=amount[b];
					amount[b]=amount[b+1];
					amount[b+1]=temp4;
				}
			}
		
			cout<<setw(2)<<agent[i]<<"              "<<setw(2)<<sales[i]<<"          "<<setw(7)<<value[i]<<"        "<<setw(7)<<amount[i]<<endl;

and its still not working, this is the output:
http://img819.imageshack.us/i/travelagentoutput.png/
it just seems that some of the sales and values and amounts get sorted, but others seem to appear in the wrong place,one example is that 3 agents appear but there should only be two, also the sales and values are all the same but should be different, i have posted my input in an earlier reply

use cout 's to follow your program:

Is your input completely correct? Display what you read and verify it's accurate.

Is your sort testing the correct values? Again, display what you are testing and at least whether or not the values get swapped. You probably don't need to display each value being swapped, just that you're swapping.

Watch for an unexpected value, like going through a loop too many times, or not enough times.

Part of the problem is you should wait until all of the value are read in, and then sort them. You're sorting the empty spots in your array around and potentially overwriting them with the next input.

How do I make it wait then read the values in? I'm not sure how that would be done.

1. Write a loop to go through the entire file and populate the arrays.
2. After the loop in step 1 finishes then write the sorting function.

What you have done now is that you have combined step 1 and step 2

this is what i tried but it doesnt work:

void display()
{
	ins.open(in_file);
	int temp1, temp2;
	float temp3, temp4;
	int count=0;
	cout<<"                 TOTAL SALES BY AGENT"<<endl;
	cout<<"AGENT NUMBER     #SALES     VALUE/SALE     AMOUNT"<<endl;
	int i=0;
	cout<<fixed<<showpoint;
	cout<<setprecision(2);
	while(!ins.eof())
	{	
		ins>>agent[i]>>sales[i]>>value[i];
		amount[i]=sales[i]*value[i];
		count++;	
		
	}
	
	for(int a=count-2; a>=0; a--)
			for(int b=0; b<=a; b++)
			{
				if(agent[b]>agent[b+1])
				{
					SWITCH(agent[b], agent[b+1]);
					
				}
			}
	cout<<setw(2)<<agent[i]<<"              "<<setw(2)<<sales[i]<<"          "<<setw(7)<<value[i]<<"        "<<setw(7)<<amount[i]<<endl;
	i++;
	cout<<endl;
	ins.clear();
	ins.close();
}
void SWITCH(int& x, int& y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
}

Increment i on line 16 instead of count, eliminate line 30, and use another while loop (or a for loop since you know the count) to display the items.

it works!! thanks a lot
i was ignoring some of the incrementing so i think thats mostly where i went wrong, i used 'i' for most of the function when i shouldn't have

if i want to compare the int agent[] arrays and if they are the same i want to add up the amounts , how would i do this?? i need to compare them and add them because if an agent has more than one seperate sale they should be added together. i only know how to do simple comparisons of seperate char arrays, so im wondering how u compare int arrays and if they are equal then how to add up the amount arrays

for(int n=count-2; n>=0; n--)
		for(int m=0; m<=n; m++)
		{
			if(q[n]==q[m])
				r[n]+=r[m];
		}

thats what i have so far but it keeps getting numbers larger than what they should be, i've put this statement right after the while(!ins.eof()) statement

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

sorrry this is what the code should be for my last post

while(!ins.eof()) is probably your problem. This should explain your difficulty ( feof is identical to .eof )

so i have this now

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

	}

and what i want it to do is to add up the amounts if the agent number is the same, also add up the sales if the agent number is the same , and then divide the amount by sales to give the value, also once i have this all it should only display the final data for each agent, but it doesnt do this

how do i get the sales to add up, and and how to i get it to display only one of the data for each agent, the data with the total of everything
my output so far is:
http://img834.imageshack.us/i/travelagentoutput2.png/

You have 2 statements under the if condition but there is no bracket to enclose the 2 statements. Is this what you want ?

OHH YAAA! i missed that, also how do i make it so that the output only shows the final one?
if u look at this:
http://img834.imageshack.us/i/travelagentoutput2.png/
u can see that for agent 1, for example, that it shows two outputs, but it should only show the second one

Currently what you are doing is comparing to see if agent[n] == agent[m] and if so you are doing some computation and storing the results at th n the position of the amount / sales / value arrays.

But when you are printing the values of the amount / sales / value arrays do you print the values at the m th location as well ?

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.