writing functions

I need help writing the functions for this code that reads from a file. the directions are in comments below. I thought I knew what I was doing but the more I adjusted the code and added the functions the more wrong it got. help or guidance would be very appreciated because I am completely lost right now.

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



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
It stores this data in three parallel arrays


It would be better to have a struct or a class that has productID, quantity, price as members.

Then you can have an array or <vector> of this struct. use for loops to do the calculations you require.

I would make the computeTotal function return a double so you can assign it to the sum variable.

Btw a void function looks like this:

1
2
3
4
5
void MyVoidFunction ( ) { //put whatever types for arguments in the parentheses
//your code here

//no return statement because it is void
}

When dealing with file streams, always check that things have worked - eg opening files.

string productID[25];


Rather than have magic numbers like 25, make them const variables instead.

Better yet, use push_back to put things in a vector - then you don't have worry about how many there are.

Also, do better indenting of your code - it should make it easier to read.

HTH Good Luck !!!
Last edited on
I need help writing the functions for this code that reads from a file.


You've already got the input down. You just need to flesh out the functions your instructor indicated that you should.

In computeTotal: For each index from 0 to size-1, multiply the corresponding elements of quantity and price together and store it in total.

for maxSales: Set maxValIndex to 0. For each index from 1 to size-1, check whether the element at that index is greater than the element at maxValIndex and if it is, set maxValiIndex to that index. Finally return the value of the element at maxValIndex in total.
Topic archived. No new replies allowed.