So I need to do this following program and I'm totally confused at the moment. Can someone please help me? Your help will be greatly appreciated.

Objective:

1. Write a program that incorporates function modules
2. Use appropriate function declarations
3. Use function headers and write function procedure
4. Use function Calls appropriately

Problem statement:

Write a program that displays the commission of a salesman using two functions. The first function is called to prompt and get sales amount from user and the sale amount is returned to the main module. The second function is called to calculate the commission and return the value to the main module. The Main Module will display the sales and the commission.

Procedure or Details:
The following table is used to calculate the commission:

Sale Amount Commission
---------------- ---------------------------
0 – 999 3.5%
1000 – 1999 4.0%
2000 – 2999 4.5%
3000 or above 5.0%

Your program will continue asking for the sales until the user types a negative number for sales, then the program should ask the user, if he/she wants to terminate the program. The program terminates if the user answers yes or y.

Recommended Answers

All 7 Replies

Maybe you should've of asked some questions in your class.

On a different note. The forum frowns on students posting homework questions and asking for solutions. Please post what you've accomplished so far and ask specific questions about specific problems.

Your general program outline will probably look something similar to this:

#include<iostream>
using namespace std;

//Function prototypes
int prompt();
double calculate_commission(double& commission);

//main driver
int main()
{
     //local function variable declaration(s)
     double commission = 0.0;

     //main loop
     while(prompt())
     {
          calculate_commission(commission);
     }

     return 0;
}

//Function Definitions
int prompt()
{
     /* you write this */
}

double calculate_commission(double& commission)
{
     /* i helped you enough,
        now it's time to help ye'self 
     */
}

so you're just making a calculator program???

I wasn't able to that day...I didn't mean to tell me the answer, just guide.
Here's what I have so far, but it's not working correctly:

#include <iostream>
#include <iomanip>
using namespace std;

int prompt(int);
int cal(int, double);
int main ()
{
	int sam=0;
	double comm=0;
	char term='n';
	double fix;
while (term !='y')
{
	while (sam != -1)
	{
		prompt (sam); 
		cout<<sam;
		cal (sam,comm);
		cout<<comm;
	}
	cout<<"Terminate program?";
	cin>>term;
}
return 0;
}
int prompt(int sal)
{
	cout<<"Enter sales amount here:";
cin>>sal;
return (sal);
}

int cal (int sal, double per)
{
	if (sal<=999)
		per=sal*.035;
	else if (sal<=1999)
		per=sal*.04;
	else if (sal<=2999)
		per=sal*.045;
	else if (sal>=3000)
		per=sal*.05;

return (per);
}

There are several problems here. First of all, the variable "fix" is not used, so you can just delete that. Second, both of your functions, "cal" and "prompt" return a value, but to where? When those values are returned, they are just dumped into empty space! There is nothing to catch them. Whenever you return a value from a function, you have to be ready at the receiving end to catch that value in a variable, like so:

sam = prompt(sam);

The variable "sam" in your function bears no relation to the variable "sam" elsewhere in your program - they are two separate variables. So when you return a value from a function, you have to "catch" that value in some variable of your choice. A function can never change an existing variable elsewhere, because the other variables are out of scope to your function. The only exception to this is when you are using global variables.

Also, in your "cal" function you are declaring a return type of "int" but are actually attempting to return a value of type "double". The complier is converting the double to an integer so that the show can go on, but isn't the complier warning you of possible loss of data? An integer can only hold whole numbers - no decimal point.

And on your "cout" statements, you need to put some line breaks in there so everything is not all on the same line, and maybe add something like, "The sales amount is:" and "The commission is:". But I imagine that you were going to get around to that after the problems are fixed : )

And last, when you enter -1 to end the while loop, that -1 is first figured as a sales amount and consequently outputs a result to the screen the same as any other input.

Ooops - one correction:

<< A function can never change an existing variable elsewhere, because the other variables are out of scope to your function. The only exception to this is when you are using global variables. >>

Actually, a function can change variables elsewhere by the use of pointers or references. But other than that, the variables in the main function are out of scope to any other function.

You could use arguments and return values. Ed:

int tacos(int muff){
return muff;
}
int main(){
int cre;
int cfr=5;
cre=tacos(cfr);

This also works in D. I actualy had no idea that I could use return vales like that.
It's actualy not that hard, when making a function inside the pare gases put the kinds of variables and their names in the parent es and then they can be used like normal variables in the program in main you just put the variables in the pare gases when it is called.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.