What am I doing wrong? Involves Void Returning Functions

So I have to code a program that calculates the commission of various sales amounts and then displays the overall total commission, using one value returning function (to get the sales) and three void functions (one to calculate the commission, another to calculate the total commission, and one to display the total commission). So this compiles fine but the program isn't calculating any of the sales commission, only that of the negative number that I'm using as a sentinel value. What am I missing?

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
60
61
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
double getSales (double &sales, double &totalSales);
void calcComm(double sales, double &salesComm, double commRate);
void displayComm (double totalComm);
void calcTotalComm(double &totalComm, double salesComm);

int main()
{
    //declare constant and variables
    double commRate         = .10;
    double sales            = 0.0;
    double totalSales       = 0.0;
    double salesComm        = 0.0;
    double totalComm        = 0.0;
    double salesCommTotal   = 0.0;
    
    //call functions
    getSales(sales, totalSales);
    calcTotalComm (salesComm, totalComm);
    displayComm (totalComm);
    calcComm(sales, salesComm, commRate);

    system("pause");
    return 0;
}   //end of main function

//******function definitions******
double getSales(double &sales, double &totalSales)
{
    cout << "Enter sales (enter a negative number to end): ";
    cin >> sales;
    
    while (sales > 0)
    
    {
          totalSales = totalSales + sales;
          cout << "Next sales amount (enter a negative number to end): ";
          cin >> sales;
    }  
}   

void calcComm(double sales, double &salesComm, double commRate)
{
     salesComm = sales * commRate;
     cout << "Commission: " << salesComm << endl;
}    //end of calcComm function

void calcTotalComm (double &totalComm, double salesComm)
{
     totalComm = salesComm + totalComm;
}    //end of calcTotalComm function

void displayComm(double totalComm)
{
     cout << "Total Commission Amount: " << totalComm << endl;
}    //end of displayComm function
     
It compiles fine? Change your compilers. This code cann't be compiled fine. At last compiler must give warnings, but compile. But warnings are not fine :S

result of my compiler.
run1.cpp: In function ‘int main()’:
run1.cpp:19:12: warning: unused variable ‘salesCommTotal’ [-Wunused-variable]
run1.cpp: In function ‘double getSales(double&, double&)’:
run1.cpp:43:1: warning: no return statement in function returning non-void [-Wreturn-type]
Yeah, I'm using Dev C++, and I'm not getting the same warnings you're getting when I hit Compile and Run. I doubt the program's suddenly stopped working.
No, it don't it had problems from the beginning. And my compiler shows these problems very fine :D.
But here are some logic problems. Like in function
calcTotalComm (salesComm, totalComm);
What is the values you give to this function? And what she does with them? Look and you will see problem.
Yes, I'm aware my code has problems. I don't know why the same warnings aren't showing up in Dev-C++.

I can't immediately assign values to that function, the values would be the result of the user's input for sales.
When you call calcTotalComm (salesComm, totalComm) salesComm and totalComm are both zero.

totalComm = 0.0 + 0.0 = 0.0
So I made some changes:

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
#include <iostream>
#include <iomanip>
using namespace std;

//function prototypes
void calcComm(double &totalSales, double &salesComm, double commRate);
void displayComm (double &totalComm);
void calcTotalComm(double totalComm, double salesComm);

int main()
{
    //declare constant and variables
    double commRate         = .10;
    double sales            = 0.0;
    double totalSales       = 0.0;
    double salesComm        = 0.0;
    double totalComm        = 0.0;
    double salesCommTotal   = 0.0;

    cout << fixed << setprecision(2);
    
    cout << "Enter sales (enter a negative number to end): ";
          cin >> sales;
    
    while (sales > 0)
    {
          
          cout << "Next sales amount (enter a negative number to end): ";
          cin >> sales;      
    }      
    
    totalSales = totalSales + sales;
    
    //call functions
    calcComm (totalSales, salesComm, commRate);
    calcTotalComm (totalComm, salesComm);
    displayComm (totalComm);

    system("pause");
    return 0;
}   //end of main function

//******function definitions******
void calcComm(double &totalSales, double &salesComm, double commRate)
{
     salesComm = totalSales * commRate;
}    //end of calcComm function

void calcTotalComm (double totalComm, double salesComm)
{
     totalComm = salesComm + totalComm;
}    //end of calcTotalComm function

void displayComm(double &totalComm)
{
     cout << "Total Commission Amount: " << totalComm << endl;
}    //end of displayComm function 


So now when I run it I'm at least getting prompts for the sales, but it must not be storing. I don't know how to set this up so that when I call calcTotalComm the parameters aren't zero.

I'm sorry for being so slow with this! Thanks for your patience.
1
2
3
4
void calcTotalComm (double totalComm, double salesComm)
{
     totalComm = salesComm + totalComm;
}    //end of calcTotalComm function 


If you want to change totalComm you want to have it as a reference here like you did in first post.

Not sure what you want to calculate here.

salesComm is total commission on all the sales. What is totalComm to be?

EDIT:

Put this line totalSales = totalSales + sales; back where you had it in first post.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
cout << "Enter sales (enter a negative number to end): ";
          cin >> sales;
    
    while (sales > 0)
    {
          
          cout << "Next sales amount (enter a negative number to end): ";
          cin >> sales;      
    }      
    
    totalSales = totalSales + sales;

better
1
2
3
4
5
6
7
do
{
        totalSales = totalSales + sales;
	cout << "Enter sales (enter a negative number to end): ";
	cin >> sales;
}
while (sales > 0);
Last edited on
I do not see a meaning for calcTotalComm (totalComm, salesComm); as totalComm = 0, salesComm is some calculated value from user input.
totalComm = salesComm + totalComm;
is
totalComm = 245 + 0;
so
totalComm = salesComm;


Is your assignment to use all this variables and functions? Or you your self choose this? Because some variables and functions are unnecessary.
Last edited on
Yeah, the assignment requires that I have a function for calculating the total commission,
because the user is supposed to be able to input multiple amounts of sales. salesComm is supposed to calculate the commission for each sales amount, and then add the commission using totalComm.

Thanks for your help, guys. I really appreciate it.
Then you are doing it wrong. Your
calcComm (totalSales, salesComm, commRate);
calculates tatal commission, not sale commission. If you want to give sale commission this function must be in do while loop. But I do not like it :(

I did it like this

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
#include <iostream>
using namespace std;

//function prototypes
void getSales (double &);
void displayComm (double, double);

int main()
{
    //declare constant and variables
    double commRate = 0.1;
    double totalSales = 0.0;
    
    //call functions
    getSales(totalSales);
    displayComm (totalSales, commRate);

    return 0;
}   //end of main function

//******function definitions******
void getSales(double &totalSales)
{
  double sales = 0;
  do
  {
    totalSales = totalSales + sales;
    cout << "Enter sales (enter a negative number to end): ";
    cin >> sales;
  }
  while (sales > 0);
}   

void displayComm(double totalSales, double commRate)
{
     cout << "Total Commission Amount: " << totalSales * commRate << endl;
}    //end of displayComm function 
Haha, I don't like it either, but my assignment is requiring me to have three void functions and one value-returning function. Although I do see the issue with calcComm now; im going to fix that now.
Nor in first, nor in second your code is any value returning function. In first you have a function which can return value, but you use it like void function. In second code here are not any. And I do not know how will you count total***** in function. because total****** is counted in do while loop. :S

Now you have a value returning function :D
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
#include <iostream>
using namespace std;

//function prototypes
double getSales ();
void displayComm (double, double);

int main()
{
    //declare constant and variables
    double commRate = 0.1;
    double totalSales;
    
    //call functions
    totalSales = getSales();
    displayComm (totalSales, commRate);

    return 0;
}   //end of main function

//******function definitions******
double getSales()
{
  double sales = 0, totalSales = 0;
  do
  {
    totalSales = totalSales + sales;
    cout << "Enter sales (enter a negative number to end): ";
    cin >> sales;
  }
  while (sales > 0);
  return totalSales;
}   

void displayComm(double totalSales, double commRate)
{
     cout << "Total Commission Amount: " << totalSales * commRate << endl;
}    //end of displayComm function 


or

1
2
3
4
5
6
7
8
9
10
int main()
{
    //declare constant and variables
    double commRate = 0.1;
    
    //call functions
    displayComm (getSales(), commRate);

    return 0;
}   //end of main function 
Last edited on
I got it! Thanks, you're awesome!
Topic archived. No new replies allowed.