Reading from a file help finishing code

program reads from a file (sales.txt) containing records in the format string int double. the string is a productID, the int is a quantity, and the double is a price. It stores this data in three parallel arrays. program then uses function, computeTotal, to compute
the total sales for each record. it uses a second function, maxSales, to find the largest total and
the corresponding index. I have most of the code done, I just need to write the functions and make the program output in to a file report.txt ( "The biggest sale occurs for product ID **** in the amount of $****" )

I completely went blank when I started to write the functions...I know its simple I feel like I am making it more complex than it is.

Any help or guidance would be appreciated!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void computeTotal(int quantity[], double price[], double total[], int size)  


{
// write code to populate the total array here

return;   // no value is returned
}
double maxSales(double total[],int& maxValIndex, int size)  

{

// write code here to determine the max total value and the index where it occurs

return maxVal;
}int main()
{
string productID[25]; 
int quantity[25], index; 
double price[25],total[25];

ifstream indata; 
indata.open("sales.txt");

ofstream outdata; 
outdata.open("report.txt");
int i=0;
indata >> productID[i] >> quantity[i] >> price[i];
while(!indata.eof()) 
{
i++;
indata >> productID[i] >> quantity[i] >> price[i];
}
computeTotal(quantity, price, total, i); // function call
double biggestSale = maxSales(total,index,i); // function call

// output code goes here

indata.close();
outdata.close();
return 0;
}





sales.txt
IF47 3 13.95
FF32 1 4.95
GB19 4 12.00
YU82 32 .89
W4R5 6 12.49
SS44 3 129.55
QW41 5 6.75
HG65 4 3.00
K8Y6 23 99.99
WQ22 11 3.43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>
using namespace std;
const int SIZE=10;
void computeTotal(int quantity[], double price[], double total_sales[], int size) ;
int maxSales(double total[], int size);
int main()
{
	string productID[SIZE]; 
	int quantity[SIZE];
	double price[SIZE],total_sales[SIZE];

	ifstream indata; 
	indata.open("sales.txt");

	ofstream outdata; 
	outdata.open("report.txt");
	for(int i=0;i<SIZE;i++)
	{
		indata >> productID[i] >> quantity[i] >> price[i];	
	}
	computeTotal(quantity, price, total_sales, SIZE); // function call
	int max_index = maxSales(total_sales,SIZE); // function call
	cout<<fixed<<showpoint<<setprecision(2);
	cout<<"______________________________________________________________________"<<endl;
	cout<<left<<setw(4)<<"No."<<setw(13)<<"Product ID"<<"Total Sales in $"<<endl;
	cout<<"______________________________________________________________________"<<endl;
	for(int i=0;i<SIZE;i++)
	{
		cout<<left<<setw(4)<<i+1<<setw(13)<<productID[i]<<"$ "<<right<<setw(8)<<total_sales[i]<<endl;
	}
	
	cout<<endl<<endl;
	cout<<"The biggest sales occurs for "<<productID[max_index]<<" for the amount of $ "<<total_sales[max_index]<<endl;
	cout<<"                                                    ___________"<<endl;
	// output code goes here

	indata.close();
	outdata.close();
	system("pause");
	return 0;
}
void computeTotal(int quantity[], double price[], double total_sales[], int size)  
{
	for(int i=0;i<size;i++)
		total_sales[i]=quantity[i]*price[i];	
}
int maxSales(double total[], int size)  
{
	int maxValIndex=0;
	for(int i=1;i<size;i++)
	{
		if(total[i]>total[maxValIndex])
			maxValIndex=i;
	}
	return maxValIndex;
}
Topic archived. No new replies allowed.