Hi All,

I am working on an assignment for this week, and once again could use your
help in understanding concept. Basically, a company pays their salespeople
on a commision basis, I need to write a program that uses a
single-subscripted array to decide how many people are getting paid within
these ranges:
200-299
300-399 and so forth through to 1000.

So, any help you can provide, as I am not even sure how to get started on
this one. Thanks.

Use an if tree to determine the index into the array.

you can use somthing like this to loop thru your array:

``````#include <iostream>

int main ()
{
int pay[] = {500000,300000,230000,156740};
int size = sizeof(pay) / sizeof (int); // find number of elements in pay[]

for (int i = 0; i < size; i++) // loop thru …``````

It looks to me like you intend to pass earnings as a parameter to the function calculateSalary. If so, tell this to your compiler (in the function definition parameter list).

``````cout << "\nHere is the commision earned :" << calculateSalary(earnings) ;
cout << "\nHere is the total …``````

cout << "\nHere is the commision earned :" << calculateSalary(comission) ;
cout << "\nHere is the total salary of employee:" << calculateSalary(totalearned);

So you want the compiler to read your mind and return the value that you want in each case? If you …

Wll, you havent made an array for the loop to loop thru.

but when you have made the array with how mutch emploeys use, you shuld make a function that returns the pay levl.
and in that function you use the if tree testes.
and you call the function …

## All 23 Replies

Use an if tree to determine the index into the array.

you can use somthing like this to loop thru your array:

``````#include <iostream>

int main ()
{
int pay[] = {500000,300000,230000,156740};
int size = sizeof(pay) / sizeof (int); // find number of elements in pay[]

for (int i = 0; i < size; i++) // loop thru pay[]
{
// std::cout << pay[i] << std::endl;
}
}``````

Ok, I can see how the loop would work. Here's the code I have so far. I am receiving errors that certain variables are not initialized. How can I fix this? Also, where do I implement the loop or the if structure for the array? Thanks.

``````//This program computes the amount of employees who receive a salary in a set number of ranges.
//Assignment 4.10

#include <iostream>
#include <iomanip>
#include <conio.h>

using std::cout;
using std::cin;

double calculateSalary (double)
{
double basesalary;  // regular earning of each employee
double earnings;    // total amount employee brings to company
double commision;   // percentage of amount employee receives as commision
double totalearned; // total amount employee earns with commision

basesalary = 200;

commision = earnings * .09;

totalearned = commision + basesalary;

}

int main() //begins program execution
{

double earnings;
double totalearned;

cout << "Enter total sales for employee :";
cin >> earnings;

cout << "\nHere is the commision earned :" << calculateSalary(earnings) ;
cout << "\nHere is the total salary of employee:" << calculateSalary(totalearned);

getch();

}``````

It looks to me like you intend to pass earnings as a parameter to the function calculateSalary. If so, tell this to your compiler (in the function definition parameter list).

``````cout << "\nHere is the commision earned :" << calculateSalary(earnings) ;
cout << "\nHere is the total salary of employee:" << calculateSalary(totalearned);``````

Did you mean something like this?

``````totalearned = calculateSalary(earnings);
cout << "\nHere is the commision earned :" << earnings;
cout << "\nHere is the total salary of employee:" << totalearned;``````

>Also, where do I implement the loop or the if structure for the array?
You appear to have code to do what you want, except only for a single object. If you want to apply this same functionality to an array of objects, you'd want to declare the array and then have a loop execute the functionality on each member of the array.

Did you mean something like this?

``````totalearned = calculateSalary(earnings)  cout << "\nHere is the commision earned :" << earnings;
cout << "\nHere is the total salary of employee:" << totalearned;``````

>

cout << "\nHere is the commision earned :" << calculateSalary(comission) ;
cout << "\nHere is the total salary of employee:" << calculateSalary(totalearned);

But, it continues to give me an error that the function is not prepared to take 1 argument, what's the deal with this?

On your other comment, how do I run a loop array on something like this?

cout << "\nHere is the commision earned :" << calculateSalary(comission) ;
cout << "\nHere is the total salary of employee:" << calculateSalary(totalearned);

So you want the compiler to read your mind and return the value that you want in each case? If you want to change the value of a variable passed to a function without it being returned, you will need to pass it by reference.

But, it continues to give me an error that the function is not prepared to take 1 argument, what's the deal with this?

What is the name of the variable you pass to it?

On your other comment, how do I run a loop array on something like this?

First things first: get it correct, then expand it.

Wll, you havent made an array for the loop to loop thru.

but when you have made the array with how mutch emploeys use, you shuld make a function that returns the pay levl.
and in that function you use the if tree testes.
and you call the function inside a loo. somting simmular to:

``````int pay[] = {12000,15600,19000}; // employe's pay.
for (int i = 0; i < size; i++) // loop thru pay[]
{
std::cout << "employe #" << i <<" is in pay lvl " << test_pay(pay[i]) << std::endl;
}

int test_pay (int employe)
{
if(employe >=  10000)
{
return 1; // pay level 1.
}
else if(employe >= 10001 || employe <=20000)
{
return 2; // pay level 2.
}
else if (employe >= 20001)
{
return 3;
}
}``````

Ok, here is my revised code to at least get the salary of each employee, but I am running into a problem. The output does not add the commision to the totalearned. What's the problem?

``````//This program computes the amount of employees who receive a salary in a set number of ranges.
//Assignment 4.10

#include <iostream>
#include <iomanip>
#include <conio.h>

using std::cout;
using std::cin;

double calculateSalary (double TotalEarned){
// regular earning of each employee
double basesalary(200);
// total amount employee brings to company
double earnings(TotalEarned);
// percentage of amount employee receives as commision
double commision(earnings * .09);
// total amount employee earns with commision
double totalearned=(commision + basesalary);
}

int main() //begins program execution
{

double earnings(0);
double totalearned=0;

cout << "Enter total sales for employee :";
cin >> earnings;

cout << "\nHere is the commision earned : \$" << calculateSalary(earnings) ;
cout << "\nHere is the total salary of employee: \$" << calculateSalary(totalearned);

getch();
return 0;

}``````

Ok, here is my revised code to at least get the salary of each employee, but I am running into a problem. The output does not add the commision to the totalearned. What's the problem?

So you want the compiler to read your mind and return the value that you want in each case? If you want to change the value of a variable passed to a function without it being returned, you will need to pass it by reference.

I did read that, but I don't know what you mean about referencing the variable. Can you explain further?

It's hard to tell what you've learned, but I thought you might have already been introduced to such things.

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

void by_value(int parameter)
{
parameter = 4;
cout << "by_value: parameter = " << parameter << endl;
}

void by_reference(int &parameter)
{
parameter = 4;
cout << "by_reference: parameter = " << parameter << endl;
}

int main()
{
int value = -1;
cout << "main: value = " << value << endl;
by_value(value);
cout << "main: value = " << value << endl;
by_reference(value);
cout << "main: value = " << value << endl;
return 0;
}

/* my output
main: value = -1
by_value: parameter = 4
main: value = -1
by_reference: parameter = 4
main: value = 4
*/``````

Ok, thats a really simple concept, I'm not seeing how it applies. Am I missing something because it's so stinking late?

Okay -- calculateSalary returns one value. Which do you want it to be? Both commision earned and total salary[/edit]? Or do you want the function to modify one of the values passed to it and return another value?

Nevermind that last post, I don't need to show commision anyway, that was my addition. So here's my vain attempt at the array. How can I get it to show up in a table or some other format? I know I'm close.

``````//This program computes the amount of employees who receive a salary in a set number of ranges.
//Assignment 4.10

#include <iostream>
#include <iomanip>
#include <conio.h>

using std::cout;
using std::cin;

int pay[];

double calculateSalary (double totalEarned){
// regular earning of each employee
double basesalary(200);
// total amount employee brings to company
double earnings(totalEarned);
// percentage of amount employee receives as commision
double commision(earnings * .09);
// total amount employee earns with commision
double totalearned=(commision + basesalary);
}

int main() //begins program execution
{

double earnings(0);
double totalEarned=0;

cout << "Enter total sales for employee :";
cin >> earnings;

cout << "\nHere is the total salary of employee: \$" << calculateSalary(earnings);

}

int pay[] = {200,300,400,500,600,700,800,900,1000};
{
for (int = 0, int < size, int++)
if calculateSalary(earnings) < 300
return 0;
if calculateSalary(earnings) < 400
return 1;
if calculateSalary(earnings) < 500
return 2;
if calculateSalary(earnings) < 600
return 3;
if calculateSalary(earnings) < 700
return 4;
if calculateSalary(earnings) < 800
return 5;
if calculateSalary(earnings) < 900
return 6;
if calculateSalary(earnings) < 1000
return 7;
getch();
return 0;

}``````

As a newb, always use full braces because you don't know any better.

Pay attention to the complaining that your compiler does. Post any questions about what it is trying to tell you.

int is a keyword.

Try to understand previous posts.

My compiler is only giving me one issue. Missing function header, on the array line. Not sure what it is asking for.

Your main function is ended with a closing brace before your array line.

Even after removing the brace, I still get syntax errors.

What kind of a crazy function definition is this?

``````int pay[] = {200,300,400,500,600,700,800,900,1000};
{
/* ... */
return 0;
}``````

You can't use a keyword as a variable.

``for ( int = 0, int < size, int++ )``

Well, if you could help me write it, which I have been kindly asking you guys since the beginning of this thread, it wouldn't look so crazy. That was my attempt. How should it be different?

I would probably do things like this.

``````double calculateSalary(double earned)
{
return result = 200 + earned * 0.09;
}

int lookup(double salary)
{
if ( salary < 300 )
{
return 0;
}
if ( salary < 400 )
{
return 1;
}
if ( salary < 500 )
{
return 2;
}
if ( salary < 600 )
{
return 3;
}
if ( salary < 700 )
{
return 4;
}
if ( salary < 800 )
{
return 5;
}
if ( salary < 900 )
{
return 6;
}
if ( salary < 1000 )
{
return 7;
}
return 8; /* 1000 or more */
}``````

Moments after posting, I couldn't shake the feeling that someone saw this and was planning to post a binary-tree version of the lookup. ::wierd::

``````int lookup(double salary)
{
if ( salary < 300 )
{
return 0;
}
if ( salary < 1000 )
{
return ((int) salary) / 100 - 2;
}
return 8; /* 1000 or more */
}``````

Moments after posting, I couldn't shake the feeling that someone saw this and was planning to post a binary-tree version of the lookup. ::wierd::

Nah, that would be too inefficient :-D

>Moments after posting, I couldn't shake the feeling that someone
>saw this and was planning to post a binary-tree version of the lookup.
>::wierd::
That is wierd. Maybe you have binary search trees on the brain. I'm big into trees, but I would probably do it like this:

``````template <typename T, size_t size>
char (&array(T(&)[size]))[size];

int lookup ( double salary )
{
static double tbl[] = {300,400,500,600,700,800,900,1000};

for ( size_t i = 0; i < sizeof array ( tbl ); i++ ) {
if ( salary < tbl[i] )
return i;
}

return sizeof array ( tbl );
}``````
Be a part of the DaniWeb community

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