So this is due tonight, but I could still use insight on how to make it better in the future.
The code's purpose is to calculate and display each salesperson's commission, (10%) and then to add up the total commissions of each salesperson and display the total. So far, I've gotten the program to run, but for some reason the program keeps giving me 0 for the commission and the total commission. I need to figure out how to get the code to actually give me a 10% commission off of numbers like 110, 15000, and 1000000
line 25 does nothing. sales is passed to amountsold() by value and returned by value.
Nothing has changed firstCommission in the do loop so its still 0.0
You need to call calculateCommision() in the do loop. And the function needs to return a double.
All those variables in calculatCommision() evaporate after the function ends. You need to return the firstcommssion value from that function. It is NOT the same variable as the firstcommision variable in your main function. They are completely different.
I don't know what your function amountSold() is for unless its a place saver. But given the other code I don't think its a place saver but a mistake. It does nothing.
You need to review scope. The variables in functions are local in scope and disappear after the function ends. It doesn't matter if they have the same name as variables in other functions ( like main). They are distinctly different. They are local to their function. You need to return a value from those functions and assign it to a variable in your main function. Otherwise you lose it and firstCommission will remain 0.0 just as you initialized it.
#include <iostream>
#include <iomanip>
usingnamespace std;
//function prototypes
double amountSold(double sales);
double calculateCommission(double sales, double commissionPercent);
void displayCommission(double firstCommission);
void calculateTotalCommission(double totalCommission, double firstCommission);
int main()
{
//declare variables
double sales = 0.0;
double commissionPercent = 0.1;
double firstCommission = 0.0;
double totalCommission = 0.0;
char anotherSale = ' ';
//enter the variables
cout << "Would you like to see a salesperson's commission? (Y/N)" << endl;
cin >> anotherSale;
do{
cout << "Enter the total sales:" << endl;
cin >> sales;
sales = amountSold(sales); // this does nothing and can be commented out
//display first commission
firstCommission = calculateCommission(sales, commissionPercent);
totalCommission += firstCommission; //add to total commission
displayCommission(firstCommission);
cout << "Would you like to review another sale? (Y/N)" << endl;
cin >> anotherSale;
} while (toupper(anotherSale) == 'Y');
//calculate and display the total commission
// calculateTotalCommission(totalCommission, firstCommission); //this is wrong. If a function is needed (and it isn't)
// it should be in the do loop and return a double.
cout << "The total commission is:" << totalCommission << endl;
return 0;
}//end main function
//***function definitions***
double amountSold(double sales)
{
return sales;
}//end of amount sold
double calculateCommission(double sales, double commissionPercent)
{
return sales * commissionPercent;
}//end of calculate commission
void displayCommission(double firstCommission)
{
cout << "The commission for this salesperson is" << firstCommission << endl;
}//end of display commission
void calculateTotalCommission(double totalCommission, double firstCommission)
{
totalCommission += firstCommission;
}//end of total commission
Anytime! The thing is that program requires no functions. All the manipulations are basic mathematical operations, inputs and outputs. At times , spurious ( unnecessary) functions make code more readable. In this case, I think it makes the code less readable. But if its a school assignment, the purpose may be to get you used to calling functions. So if its the assignment to use functions for all of this, I can show you code to do that. But if you wrote this thinking that is the proper way to do this? No its not and I can show you the proper way to do it. Good code is clear code. Always.
Yes, thats it. But I don't know if the object of the lesson is function calls. If so, that's what its for, but if he wrote that accomplish the task, it requires no functions.