Really stuck

I have the following question I need help with completing

Write a program that calcs the average of the four quarterly sales of a company and displays both the highest sales figure as well as the average sales figure for the year. For example if the user enters these values:

458.60 , 430.00, 376.70 and 246.30

the output will be:
The highest sales figure is R458.60 with an average of R377.90
to get the value of R377.90, the four values are added and divided by 4.

it should include the following functions, which are called by the main function:

a) A float function getSales to ask the user for a sales figure, validate it and return the sales figure. The function should be called by the main function once for each of the four quarterly sales figures to be entered. Validate the input so that the program does not accept amounts less than 0.

b) A float function findHighest that is passed the four sales figures, and that then determines and returns the highest sales figure.

c) A float function calcAverage that is passed the four sales figures, and that then determines and returns the average sales figure.

d) A void function displayOutput that is passed the highest sales figure and the average sales; it then displays the highest sales figure as well as the average sales figure, to two decimal digits after the decimal point.

the incomplete program is below along with the data that must be used for the program.

*****data to use*****

sales 1 * 6565.76 * 2443.67 * 8010.20 * 7343.45
sales 2 * 6653.80 * 3843.80 * 8444.80 * 6943.45
sales 3 * 7233.40 * 5232.68 * 8123.30 * 2645.12
sales 4 * 6543.00 * 2843.65 * 7265.50 * 6388.30

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
//This program displays the division with the highest sales figure.
#include <iostream>
using namespace std;

//You must add the four functions here

int main()
{
   float sales1,
         sales2,
         sales3,
         sales4;
   float averageSales;
   float highestSales;

   for (int i = 0; i < 4; i++)
   {
      //Get the sales for each division.
      sales1 = getSales();
      sales2 = getSales();
      sales3 = getSales();
      sales4 = getSales();

      //Find the highest of the four sales figures
      highestSales = findHighest(sales1, sales2, sales3, sales4);

      //Find the average of the four sales figures
      averageSales = calcAverage(sales1, sales2, sales3, sales4);

      //Display the highest sales figure as well as the average
      displayOutput(highestSales, averageSales);

   }

return 0;
} //End of main 
Last edited on
The functions will look like this:

1
2
3
4
returnType functionName(Arg1Type arg1, Arg2Type arg2)
{
    // your code here
}

I feel like throwing in the towel

I am trying to learn this on my own because I believe that it could be valuable.

I have tried so many different things yet don't seem to get the proper results. Just don't understand!!!!!!!!!!!!!!! (going to pull out my hair) lol

Would you mind typing out the first float function for me to get the input from the user and i am sure that i will understand the others fine once I've seen the first. I know how to validate so that is cool. I am almost sure that I can add it to the function without effecting anything.

this is what i have for the first function without the validation code.
when i run this with the function call in the main() it asks me those 4 inputs 4 times. is that right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

float getSales()
{
    float sales1;
    float sales2;
    float sales3;
    float sales4;
    
    cout << "Please enter sales1 figure: ";
    cin >> sales1;
    cout << "Please enter sales2 figure: ";
    cin >> sales2;
    cout << "Please enter sales3 figure: ";
    cin >> sales3;
    cout << "Please enter sales4 figure: ";
    cin >> sales4;
    
    cout << " " << sales1 << " " << sales2 << " " << sales3 << " " << sales4;
    cout << endl;
    
    return sales1, sales2, sales3, sales4;
};
float getSales()

This is a function that takes no arguments and returns one float value.


return sales1, sales2, sales3, sales4;

This is the same as return sales4; . Look up the comma operator in your favorite reference.
You'll notice in lines 19-22 of the original program, getSales is called 4 times. So your function can be rewritten as:

1
2
3
4
5
6
7
8
9
10
11
12
float getSales()
{
    float sales;

    cout << "Please enter sales figure: ";
    cin >> sales;
    
    cout << " " << sales << endl;

    return sales;

};


Edit: I don't think you really need the cout statement, but you had it in your code, so I left it.
Last edited on
Thanks to everybody i now understand how the calling of the functions work:)

doug4.

you are the man:)
my first float function is going like a boing and i am going to attempt the rest of the program now.
wesfos.. Could you please copy-paste your finale functions here.
I was following this post and this info could help me understand too. ;)
This is my program, without the for loop from the main.

#include <iostream>

using namespace std;

float getSales()
{
float sales;

cout << "Please Enter the Sales: ";
cin >> sales;

return sales;
}

float calcAverage(float sales1, float sales2, float sales3, float sales4)
{
int n = 4;
float averageSales;

averageSales = (sales1 + sales2 + sales3 + sales4) / 4;

return averageSales;

}

float findHighest(float sales1, float sales2, float sales3, float sales4)
{
int counter = 4;

float highestSales = sales1;

if (sales2 > highestSales)
highestSales = sales2;
if (sales3 > highestSales)
highestSales = sales3;
if (sales4 > highestSales)
highestSales = sales4;


return highestSales;
}

float displayOutput(float highestSales, float averageSales)
{
cout << "Sales Average are: " << averageSales << endl;

cout << "highestSales is: " << highestSales << endl;

return averageSales, highestSales;
}
int main()
{
float sales1,
sales2,
sales3,
sales4;
float averageSales;
float highestSales;

//Get the sales for each division.
sales1 = getSales();
sales2 = getSales();
sales3 = getSales();
sales4 = getSales();

//Find the highest of the four sales figures
highestSales = findHighest(sales1, sales2, sales3, sales4);

//Find the average of the four sales figures
averageSales = calcAverage(sales1, sales2, sales3, sales4);

//Display the highest sales figure as well as the average
displayOutput(highestSales, averageSales);


system("pause");
return 0;
} //End of main
Topic archived. No new replies allowed.