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)