Help.. Stuck on a section of my C++ coding

Hi,

Please help!

I am stuck at one point on my c++ coding. The program is to read monthly sales into a dynamically allocated array. Te program will input size of the array from the user. It will call a function that will find the yearly sum (the sum of all the sales). It will also call another function that will find the average.

Here is where I am at. The function has three parameter and I am not sure how to send the argument to the function. Any help will 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
#include<iostream>

using namespace std;

void yearTotal (float *sales, int saleSize, float & sum);
void yearAverage(float sum, int saleSize, float & avg);

int main ()
{
	int count;
	int months = 0;
	float *sales = nullptr;
	sales = new float[months]
	float total;
	float average;
	

	cout << "Please input the number of monthly sales to be input";
	cin >> months;
	
	for (count = 0; count < months; count++)
		cout << "Please input the sales for month" << count << endl;
		cin >> yearTotal(sales + count)
		
return 0;

}


Last edited on
Please use code tags.
http://www.cplusplus.com/articles/jEywvCM9/

int cout;
Typo?

1
2
3
int months;					// uninitialised
float *sales = nullptr;
sales = new float[months]	// undefined behaviour 


count << "Please input the number of monthly sales to be input";
Typo?

1
2
3
4
5
// months is uninitialised
for (count = 0; count < months; count++) {
cout << "Please input the sales for month" << count << endl;
cin >> yearTotal(sales + count)
}


1
2
3
4
yearTotal(sales + count)

// prototype
void yearTotal (float *sales, int saleSize, float & sum)

You haven't supplied enough arguments.


The program is to read monthly sales into a dynamically allocated array.

I don't see you reading anything into your array.

For every new there must be a delete.
Last edited on
I think you have made a dozen of mistakes in your code. I reviewed the code for u. wish you can benefit from it.
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
#include<iostream>

using namespace std;

void yearTotal(float * sales, int saleSize, float & sum);
void yearAverage(float sum, int saleSize, float & avg);

int main()
{
    // 1. here is a typo
    //int cout;
    int count;
    int months;
    // 2. use new is ok, but not recommand, you have to free it by yourself
    float * sales = nullptr;
    // 3. months is uninitialized now, using it is bad!
    //sales = new float[months];
    float total;
    float average;
    // 4. typo? it should be std::cout
    //count << "Please input the number of monthly sales to be input";
    std::cout << "Please input the number of monthly sales to be input";
    cin >> months;
    sales = new float[months];

    for (count = 0; count < months; count++)
    {
        cout << "Please input the sales for month" << count << endl;
        // 5. you need to read from std::cin now
        std::cin >> sales[count];
    }

    // call yearTotal now
    //cin >> yearTotal(sales + count)
    yearTotal(sales, months, total);
    yearAverage(total, months, average);
    std::cout << "year total: " << total << std::endl
              << "year average: " << average << std::endl;
    return 0;
}

void yearTotal(float * sales, int saleSize, float & sum)
{
}
void yearAverage(float sum, int saleSize, float & avg)
{
}


Edit: the code is still not perfect. hoping you can perfect it by yourself.
Last edited on
Thank both of you for your advise!

Sorry for the mess. I was trying to write the code on my android phone. It auto corrected cout to count. Very embarrassing.

It was really mind boggling because I haven't seen a three parameter function. When I first saw this, I wasn't sure how to pass the arguments to the function.

Topic archived. No new replies allowed.