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?
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.
#include <iostream>
#include <iomanip>
usingnamespace 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.
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.
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 :(
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
#include <iostream>
usingnamespace 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