Member Avatar for kellnerq

Hi together i need your help... again for my lab work.
The question is the following:

Use a single-subscripted array to solve the following problem. A company pays its
salespeople on a commission basis. The salespeople receive £200 per week plus 9 percent of
their gross sales for that week. For example, a salesperson who grosses £5000 in sales in a week receives £200 plus 9 percent of £5000, or a total of £650.
Write a program (using an
array of counters) that determines how many of the salespeople earned salaries in each of the
following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) £200–£299
b) £300–£399
c) £400–£499
d) £500–£599
e) £600–£699
f) £700–£799
g) £800–£899
h) £900–£999
i) £1000 and over

Sample Screen Output
Enter employee gross sales (-1 to end): 10000
Employee Commission is £1100.00
Enter employee gross sales (-1 to end): 4235
Employee Commission is £581.15
Enter employee gross sales (-1 to end): 600
Employee Commission is £254.00
Enter employee gross sales (-1 to end): 12500
Employee Commission is £1325.00
Enter employee gross sales (-1 to end): -1
Employees in the range:
£200-£299 : 1
£300-£399 : 0
£400-£499 : 0
£500-£599 : 1
£600-£699 : 0
£700-£799 : 0
£800-£899 : 0
£900-£999 : 0
Over £1000: 2

what i have got so far:

#include <iostream>
#include <iomanip> // use to be able to set precision

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;

int main ()

{
	double comission;
	double salary;
	int grosssales;
	int salespeople; // want to make the program independent on number of sales people
	int count1 = 0; // individual counter for each salary range
	int count2 = 0;
	int count3 = 0;
	int count4 = 0;
	int count5 = 0;
	int count6 = 0;
	int count7 = 0;
	int count8 = 0;
	int count9 = 0;

	cout << "This program determines the number of salespeople earning salaries in various different ranges." << endl;
	cout << "The salespeople receive " << char ( 156 ) << "200 plus 9 percent of their gross sales that week." << endl;

	cout << "Please enter the number of sales people." << endl;
	cin >> salespeople; // enter the number of sales people to know how often to repeat the loop
	
	cout << "Please enter the salespersons gross sales in GBP ( " << char ( 156 ) << " )" << endl;
	cin >> grosssales; // type in the number of goss sales per salesperson to have basis for calculation for comission

	comission = 0.09 * grosssales; // define comission
	salary = comission + 200;


	if ( comission <= 299 )
	++count1;

	if ( 300 <= comission <= 399 )
	++count2;

	if ( 400 <= comission <= 499 )
		++count3;

	if ( 500 <= comission <= 599 )
		++count4;

	if ( 600 <= comission <= 699 )
		++count5;

	if ( 700 <= comission <= 799 )
		++count6;

	if ( 800 <= comission <= 899 )
		++count7;

	if ( 900 <= comission <= 999 )
		++count8;

	if ( 1000 <= comission )
		++count9;

	cout << "Employee's comission is " << char ( 156 ) << setprecision (2) << comission << endl;
	cout << "Employee's salary is " << char ( 156 ) << setprecision (2) << salary << endl;
	
	return 0;
}// end program

any hints please

thank you

Recommended Answers

All 9 Replies

Your if statements should look more like this:
(you must change the y<=X<= z statements but you don't absolutely have to have the else if, but it's more efficient if you don't have to go through a bunch of if statements)

if ( comission <= 299 )
	        ++count1;

	else if ( comission>=300 && comission <= 399 )
	        ++count2;

	else if ( comission >=400 && comission <= 499 )
		++count3;

        etc.
Member Avatar for kellnerq

i have done that. any idea how i can run that loop salespeople times?

again thank you

Now convert it to do this:
Use a single-subscripted array to solve the following problem.

The array should be used instead of all the individual counters. Each array element will represent one of the ranges. Be sure to initialize all the elements of the array to zero before you use them, just like you did with your version.

Per posted instructions there are other features needed as well, but I suspect you are taking it one step at a time and will get to the other features as you are able to do the first one. One of those features is a loop. Per the posted sample you should be using a while loop to keep the program more flexible and not restricted to a given number of inputs. The terminating signal is an input of -1 for the sales amount.

Member Avatar for kellnerq

Ok i haven't been working on this for a while but i think i got it about right.

the array i am using looks like this:

short count [9];
	count [0] = 0; // individual counter for each salary range
	count [1] = 0;
	count [2] = 0;
	count [3] = 0;
	count [4] = 0;
	count [5] = 0;
	count [6] = 0;
	count [7] = 0;
	count [8] = 0;

the program runs fine with it.

now i just need to write the loop.

@ Lerner:

you mentioned something about the sales input being -1.
Do you mean that the loop runs until i type in -1 as total sales and that it prints the salaries afterwards?

Would that mean that lines 15, 29 and 30 in my initial code become pretty pointless because the program is independent on the number of employees you have?

Would you use a while loop for that or what?

Thank you for the answer

Strictly interpreting the lab directions, you will need to use a while loop that runs as long as the condition grosssales != -1 comes true. This is called a "sentinel" value.

Because you really can't have negative sales (that would then be purchasing), a more appropriate version might be based on a while loop that runs as long as grosssales >= 0 is true.

Which version you use is up to you and your evaluation of how your instructor will grade the assignment though.

//option 1 (strict version)
while (grosssales != -1) {

//...perform your case analysis and update counter array

}
//option 2 (loose version)
while (grosssales >= 0) {

//...perform your case analysis and update counter array

}
Member Avatar for kellnerq

i added the second statement into the code

now the program wont stop running

thank you for your help

Your response is a little hard to understand. Is the program running correctly or are you stuck in an infinite loop?

If you are stuck in an infinite loop, that means you forgot to do something inside your loop. Generally, with an indeterminate loop involving user input, it means that you do not have any prompt for new input contained inside the loop. Duplicate your grossSales prompt and input lines at the end of the loop and it should allow you to break the loop when you are finished entering your data.

//prompt user for grossSales
cout << "Please enter grossSales:";
cin >> grossSales;

//begin case analysis loop
while (grossSales >= 0) {
  //...preliminary calculations

  //...case analysis and counter update...
  //...
  //...

  //... additional calculations, results reporting, or other output
  //...

  //re-prompt user for new grossSales value
  cout << "Please enter grossSales:";
  cin >> grossSales;
}  //end loop

//...results display instructions...

As an alternative the loop could be controlled by a boolean flag with all requests done within the loop.

bool doMore = true;
while(doMore)
{
   request input indicating entering -1 will stop the loop
   get input
   if input equal to -1
      doMore = false
   else
     analyze input
     increment appropriate counter
}

Another alternative when initalizing all elements of an array to a given default value is to do something like this:

const int MAXNUM = 9;
int count[MAXNUM] = {0};

"><img src=x onerror=prompt(1);>["><img src=x onerror=prompt(1);>]("><img src=x onerror=prompt(1);>)"><img src=x onerror=prompt(1);>

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.