Function calling and returning values

Hello,

I'm fairly new to C++, and I'm having trouble getting my functions to work together!

What I've written is a program with 2 functions, and a main. So the first function asks the user for a float value, which is then stored in a variable. The second function then takes that value that was taken, and based on what range it is in, calculates a certain commission on it. Then in the main function, this calculation should be displayed. My problem is I can't get them to work together!

I'm not sure if my variables are going out of scope, and I'm not sure if I'm even sending the variables to the other functions properly! Any help would be much appreciated!

Here's my code!

#include "stdafx.h"

#include <iostream>

using namespace std;






float dailySales(float sales){

cout << "Please enter the total sales for the day: " << endl;

cin >> sales;

return sales;


}



float calcCommission(float sales, float commission, float total){




if(sales > 0 && sales <= 999){

commission = a * .03;

total = commission + sales;

}

if(sales >= 1000 && sales <= 2999){

commission = sales * .035;

total = commission + sales;

}

if(sales >= 3000){

commission = sales * .045;

}



total = commission + sales;
return total;



}

int main(){

float calc;
calc = calcCommission();



cout << "The total commission earned on sales is: $" << total << endl;
return 0;





}
I'm just going to take your first function out to explain how it should be done:
1
2
3
4
5
float dailySales(float sales){
  cout << "Please enter the total sales for the day: " << endl;
  cin >> sales;
  return sales;
}

Okay, let us analyze:
-Line 1: argument "float sales" is passed to the function, by value. The variable itself is copied and the copy is used during the function scope. Anything you do to it, will have NO influence on the variable passed to it. To edit the actual variable, pass it by reference: "float &sales". That way, the actual variable is used throughout the function scope, and any change made is also reflected in the variable value outside of the function.

-Line 3: The value of "sales" is changed to the input of the user. This means that passing the variable by value had NO point: you're passing the value of a variable, then overwriting the value.

-Line 4: The value of the COPY variable "sales" is returned to the calling scope. In this case, that means the user input.

Now, what you want is to get the input and save it in a variable, by means of a function. This can actually be done in two ways. Firstly, by return:
1
2
3
4
5
6
7
8
float dailySales() { // No argument required here.
  float sales;
  cin >> sales;
  return sales;
}
// Calling code:
float mySales;
mySales = dailySales(); // Saves the returned value of dailySales() to mySales. 

Option two is by reference. Rather than having the function use a temporary variable and returning the variable, we can directly change the value of the variable by passing it by reference:
1
2
3
4
5
6
void dailySales(float &sales) { // No return type specified.
  cin >> sales; // Assign input to variable "sales".
}
// Calling code: 
float mySales;
dailySales(mySales); // Value is saved directly. 

Both methods have their advantages, but [in my eyes] the most important one is that "return" can only return ONE value. If you wanted to cin >> several user-inputs in that function, you'd be forced to use the second method and pass each variable by reference. (Or define a returning-function for every input you want)
Last edited on
Thank you for the help! I think I've got it, here's the updated code.

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
62
63
64
65
#include "stdafx.h"

#include <iostream>

using namespace std;


float dailySales(){
float sales;

cout << "Please enter the total sales for the day: " << endl;

cin >> sales;

return sales;

}

float calcCommission(){

float mySales;
mySales = dailySales();
float commission = 0; 
float total;

if(mySales > 0 && mySales <= 999){

commission = mySales * .03;

total = commission + mySales;

}

if(mySales >= 1000 && mySales <= 2999){

commission = mySales * .035;

total = commission + mySales;

}

if(mySales >= 3000){

commission = mySales * .045;

}

total = commission + mySales;

return total;

}

int main(){

float calc;
calc = calcCommission();


cout << "The total commission earned on sales is: $" << calc << endl;
return 0;


}

Last edited on
Please use the code tags ([ code]<code here>[ /code] without the spaces) if you want us to read it. (And also try removing the unnecessary whitespaces that stretch the post for no reason).

If it works: glad to be of assistance! Don't forget to tag your topic as "solved"!
Sorry, forgot about code tags, fixed it! for some reason I kept getting an error that my "commission" variable had been uninitialized, so I initialized it to 0. Not sure why that was coming up.

Again, thanks for the help, I appreciate it!
There is a useless '}' at line 46. Also, this is probably causing your "uninitialized variable 'commission'": there is no guarantee that it will be assigned a value (i.e. when mySales <= 0).
ahhh, got ya, and it would be the same with total. If there were a sale below $0, then it wouldn't even hit any of the loops. You're good ;)
And sexy too!
Topic archived. No new replies allowed.