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
|
This program reads from a file, sales.txt, containing multiple records of the format:
string int double
where the string is a productID, the int is a quantity, and the double is a price.
It stores this data in three parallel arrays. The program then uses a function, computeTotal, to compute
the total sales for each record. Next, it uses a second function, maxSales, to find the largest total and
the corresponding record index.
Finally, the message "The biggest sale occurs for product ID **** in the amount of $****" is written to a
file, report.txt, where of course the **** are replaced by the correct values.
Obviously, a great deal of the code is already provided for you. Your task is to write code to complete the
function bodies and to produce the correct output to the file.
*/
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void computeTotal(int quantity[], double price[], double total[], int size) // notice void return type
{
// write code and place here to correctly populate the total array
return; // notice no value is returned
}
double maxSales(double total[],int& maxValIndex, int size) // notice the call by reference parameter
{
// write code and place here to correctly 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; // this is the way we declare input streams
indata.open("sales.txt");
ofstream outdata; // this is the way we declare output streams
outdata.open("report.txt");
int i=0;
indata >> productID[i] >> quantity[i] >> price[i];
while(!indata.eof()) // reading in the data to parallel arrays
{
i++;
indata >> productID[i] >> quantity[i] >> price[i];
}
computeTotal(quantity, price, total, i); // function call
double biggestSale = maxSales(total,index,i); // function call
// place appropriate code here to produce correct output
indata.close();
outdata.close();
return 0;
}
|