|
 |
|
 |
 |
07-13-2005, 08:54 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Jul 2005
Posts: 6
|
Array Problem
Hey Guys,
I am having a problem getting this program to work, can you help? It needs to return the values of the array to show which employees have made what amounts within the certain guidelines. Having trouble figuring out how array should be declared and written. Thanks.
Code:
//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);
return totalearned;
}
int main() //begins program execution
{
double earnings(0);
double totalEarned=0;
while (earnings =! -1)
cout << "Enter total sales for employee (Enter -1 to stop and tabulate) :";
cin >> earnings;
cout << "\nHere is the total salary of employee: $" <<
calculateSalary(earnings);
}
int pay[7]
{
switch (earnings)
case < 300 :
pay[0] = pay[0] + 1;
break;
case (< 400) (>= 300):
pay[1] = pay[1] + 1;
break;
case (< 500) (>=400):
pay[2] = pay[2] + 1;
break;
case (<600) (>=500):
pay[3] = pay[3] + 1;
break;
case (<700) (>=600):
pay[4] = pay[4] + 1;
break;
case (<800) (>=700):
pay[5] = pay[5] + 1;
break;
case (<900) (>=800):
pay[6] = pay[6] + 1;
break;
case (<1000) (>=900):
pay[7] = pay[7] + 1;
break;
}
__________________
|
|
|
07-13-2005, 11:18 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Code:
#include <iomanip>
#include <conio.h>
why are you using these ?? There is no where in your code they are required..
Anyway I'll have a look at it later today, when I get the time, and I'm sure Valmont will too, if he stopes by..
|
|
|
07-14-2005, 05:16 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Sorry your code is in such a state, that I can't seem to see what it should do. your switch() is outside the main, even outside any function call, it isn't even a valid switch() statement, your declaring alot of variables, which you're not even usign further than declaring them...
I'm working on an example which will do what I think you might wanted this to do, but since your description is something like "return the values of the array to show which employees have made what amounts within the certain guidelines." that isn't very descriptive..
I guess my example is going to be none of what you'd expect to be using..
Last edited by redhead : 07-14-2005 at 05:44 AM.
|
|
|
07-14-2005, 05:46 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Here is my example.. It is nothing like the one you're working on..
Code:
/* This program computes the amount of employees
* who receive a salary in a set number of ranges.
*
* Assignment 4.10
*/
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
double calculateSalary (double totalEarned){
return ((totalEarned*0.09) + 200.0);
}
int main() //begins program execution
{
double earnings = 0.0;
/* allways make room for one more than your index count */
int pay[]= {0,0,0,0,0,0,0,0}; /* for ease we initialize it as 0 */
while (true)
{
cout << "Enter total sales for employee";
cout << " (Enter -1 to stop and tabulate) : ";
cout.flush();
cin >> earnings;
if(earnings == -1.0)
break;
for(int i = 0, amount=300; i < 8; ++i, amount+=100)
if(earnings < amount)
{
pay[i] = pay[i] + 1;
break;
}
cout << "Here is the total salary of employee: $"
<< calculateSalary(earnings) << endl;
}
cout << "The earnings are as follows:" << endl;
for(int i=0, amount=300; i < 8; ++i, amount+=100)
cout << "(" << amount-100 << " <= Amount < " << amount
<< ") Count: " << pay[i] << endl;
return 0;
}
|
|
|
07-14-2005, 02:09 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Jul 2005
Posts: 6
|
Nice Example
Hey, thank you for your help in understanding the concept. I am sorry for being so indescript at first. This is basically what I need the program to do. It has to compute the salary of an employee who makes $200 base salary, plus 9 percent commision on all sales. Then it needs to truncate the info into an array that prints how many employees have salaries within the following ranges. 200-299, 300-399, 400-499 and so forth through to 1000. Would your example allow for this?
__________________
|
|
|
07-15-2005, 12:56 AM
|
#6 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
My example will do that, but it will print the info in a confusing way.
For this I would suggest to use vectors rather than arrays, but if the section you've just covered with this assignment, is about arrays then it is expected to be achieved with arrays.
I'll have a crack at it later today, but don't expect anything within the next 8 - 10 hours.
|
|
|
07-15-2005, 05:17 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Jul 2005
Posts: 6
|
Thanks
Thank you. Unfortunately, it does have to be done using a single subscripted array. But I will be thankful for any help you can offer. Getting kinda desperate here.
__________________
Last edited by logic321 : 07-15-2005 at 11:19 PM.
|
|
|
07-15-2005, 11:05 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Jul 2005
Posts: 6
|
Update
Did you come up with anything? Just checking.
__________________
|
|
|
07-16-2005, 07:01 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Quote:
|
Did you come up with anything?
|
Yes, and I'm working on it, but you could take my previus solution and play around with the output, in order to make it more acceptable. Look at precision, fill and width
The solution I'm working on, will probably end up beeing nothing expected for this assignment, at the moment I'm working on a layout where you would store the employees with information on their sales and their commisions/salary for those sales, in the end you will be able to require a single employee to be shown and information on where within the other employees that person resides. In respect to Sales/commision/Salary aswell as a print of top 3 employees and so forth.
Aswell as get a total printout where every sale can be shown asweell as an indication of teh employee with max/min sale adn an average sale from every employee.
|
|
|
07-18-2005, 01:58 AM
|
#10 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
A solution with the use of vectors
If you were wondering, this is my solution where I use vectors instead of arrays.
Like I told you, it's probably of no use to you.
Makefile
Code:
CC=g++
MAKE=make
CFLAGS= -Wall -O2
LINKFLAGS=
LIBS=
EXE= arrays
SRC=employee.cpp arrays.cpp
OBJ=$(SRC:.cpp=.o)
all: $(SRC) $(EXE)
arrays: $(OBJ) $(EXE)
$(CC) $(CFLAGS) -o $(EXE) $(OBJ) $(LINKFLAGS) $(LIBS)
clean:
rm -f *~ core \#*\# .\#* *.o $(EXE)
employee.h
Code:
#ifndef __EMPLOYEE__H__
#define __EMPLOYEE__H__
#include <vector>
#include <string>
class employee
{
private:
std::string name;
std::vector <double> sales;
std::vector <double> earnings;
/* not allowed to make an employee without a name */
employee(void){};
public:
employee(std::string n){name=n;};
void add_sale(double sale);
std::string get_name(void){return name;};
void show_sales(int info);
double get_min(int choice);
double get_max(int choice);
double get_total(int choice);
double get_average(int choice);
};
#endif
employee.cpp
Code:
#include "employee.h"
#include <iostream>
#include <iomanip>
void employee::add_sale(double earning)
{
sales.push_back(earning);
earnings.push_back((earning*0.09) + 200.0);
}
void employee::show_sales(int info)
{
double total = 0.0, min = sales[0], max = 0.0, temp = 0.0, count = 0.0;
std::vector <int> ranges(9,0);
switch(info)
{
case 1:
std::cout << " Sales & Earnings for ";
std::cout << name << std::endl;
/* This could have been accomplished by calling
* this->get_max();
* this->get_min();
* this->get_total();
* and this->get_average();
* But since that would require atleast 3 loops through
* the vector, it is easier to calculate everything
* in a single loop
*/
for(unsigned int i = 0; i < sales.size(); ++i)
{
if(sales[i] < min)
min = sales[i];
if(max < sales[i])
max = sales[i];
if(min > max)
{
temp = max;
max = min;
min = temp;
}
total += sales[i];
++count;
}
std::cout << " .----------.----------.----------.----------." << std::endl;
std::cout << " | total | min. | max. | avg. |" << std::endl;
std::cout << ".----------------|----------|----------|----------|----------|" << std::endl;
std::cout.precision(2);
std::cout.flags(std::ios::fixed);
std::cout << "| Sales |";
std::cout << " $";
std::cout.width(7);
std::cout << total << " |";
std::cout << " $";
std::cout.width(7);
std::cout << min << " |";
std::cout << " $";
std::cout.width(7);
std::cout << max << " |";
std::cout << " $";
std::cout.width(7);
std::cout << (total / count) << " |" << std::endl;
/* This could have been accomplished by calling
* this->get_max();
* this->get_min();
* this->get_total();
* and this->get_average();
* But since that would require atleast 3 loops through
* the vector, it is easier to calculate everything
* in a single loop
*/
min=earnings[0];
max=0.0;
total=0.0;
for(unsigned int i = 0; i < earnings.size(); ++i)
{
if(earnings[i] < min)
min = earnings[i];
if(max < earnings[i])
max = earnings[i];
if(min > max)
{
temp = max;
max = min;
min = temp;
}
total += earnings[i];
}
std::cout << "|----------------|----------|----------|----------|----------|" << std::endl;
std::cout.precision(2);
std::cout.flags(std::ios::fixed);
std::cout << "| Earnings |";
std::cout << " $";
std::cout.width(7);
std::cout << total << " |";
std::cout << " $";
std::cout.width(7);
std::cout << min << " |";
std::cout << " $";
std::cout.width(7);
std::cout << max << " |";
std::cout << " $";
std::cout.width(7);
std::cout << (total / count) << " |" << std::endl;
std::cout << "'----------------'----------'----------'----------'----------'" << std::endl;
break;
default:
std::cout << " Ranges of Sales for ";
std::cout << name << std::endl;
for(unsigned int i = 0; i < sales.size(); ++i)
for(unsigned int j=0, amount=(1000-(ranges.size()-1)*100); j < ranges.size(); ++j, amount+=100)
if(sales[i] >= amount-100 && sales[i] < amount)
{
ranges[j] = ranges[j]+1;
break;
}
std::cout << ".------------.-------.-----------.-------.-----------.-------." << std::endl;
std::cout << "| Range | Sales | Range | Sales | Range | Sales |" << std::endl;
std::cout << "|------------|-------|-----------|-------|-----------|-------|" << std::endl;
for(unsigned int j=0, amount=(1000-(ranges.size()-1)*100); j < ranges.size(); ++j, amount+=100)
{
if(!(j%3))
std::cout << "| ";
else
std::cout << "| ";
std::cout << amount-100 << " - " << amount-1 << " | ";
std::cout.width(5);
std::cout << ranges[j] << " ";
if(!((j+1)%3))
std::cout << "|" << std::endl;
}
std::cout << "'------------'-------'-----------'-------'-----------'-------'" << std::endl;
break;
}
std::cout << std::endl;
}
double employee::get_min(int choice)
{
double min;
if(choice == 1)
{
min = earnings[0];
for(unsigned int i = 1; i < earnings.size(); ++i)
{
if(earnings[i] < min)
min = earnings[i];
}
}
else
{
min = sales[0];
for(unsigned int i = 1; i < sales.size(); ++i)
{
if(sales[i] < min)
min = sales[i];
}
}
return min;
}
double employee::get_max(int choice)
{
double max;
if(choice == 1)
{
max = earnings[0];
for(unsigned int i = 1; i < earnings.size(); ++i)
{
if(earnings[i] > max)
max = earnings[i];
}
}
else
{
max = sales[0];
for(unsigned int i = 1; i < sales.size(); ++i)
{
if(sales[i] > max)
max = sales[i];
}
}
return max;
}
double employee::get_total(int choice)
{
double total = 0.0;
if(choice == 1)
for(unsigned int i = 0; i < earnings.size(); ++i)
total += earnings[i];
else
for(unsigned int i = 0; i < sales.size(); ++i)
total += sales[i];
return total;
}
double employee::get_average(int choice)
{
double total = this->get_total(choice);
if(choice == 1)
return (total / (double) earnings.size());
else
return (total / (double) sales.size());
}
arrays.cpp
Code:
/*
* A program to calculate the number of employees
* who receive a salary in a set number of ranges.
* Orriginaly assignment 4.10, but for complete usage
* has been remodeled.
*
* Assignment 4.10.1 AKA: Assignment 4.10 with vectors
*/
#include "employee.h"
#include <iostream>
std::string get_employee(void);
double get_sale(std::string name);
void reset_istream(void);
void wait_for_enter(void);
void show_usage(char* name);
int show_menu(void);
int main(void)
{
double sale;
int choice;
std::string name;
std::vector <employee> employees;
char ch;
/* have a conversation with the user, on what they like to see */
while(true)
{
choice = show_menu();
switch(choice)
{
case 1:
/* Lets create the employees in this input round */
while(true)
{
name = get_employee();
if(name == "q" || name == "Q")
break;
employee temp(name);
while(true)
{
sale = get_sale(name);
if(sale == 0.0)
break;
temp.add_sale(sale);
}
employees.push_back(temp);
}
break;
case 2:
/* show the earnings along with sales */
for(unsigned int i=0; i < employees.size(); ++i)
{
employees[i].show_sales(1);
if(i%2)
{
wait_for_enter();
reset_istream();
}
}
wait_for_enter();
reset_istream();
break;
case 3:
/* show the ranges of sales */
for(unsigned int i=0; i < employees.size(); ++i)
{
employees[i].show_sales(2);
if(i%2)
{
wait_for_enter();
reset_istream();
}
}
wait_for_enter();
reset_istream();
break;
}
/* exit */
if(choice == 4)
break;
}
return 0;
}
std::string get_employee(void)
{
std::string name;
std::cout << "Enter name of employee, use Q to quit: ";
std::cout.flush();
getline(std::cin, name);
return name;
}
double get_sale(std::string name)
{
double sale;
std::cout << name << "'s " << "Sale (use 0 to stop): $";
std::cout.flush();
std::cin >> sale;
reset_istream();
return sale;
}
int show_menu(void)
{
int answer;
start_here:
std::cout << "\t\t.------------------------------." << std::endl
<< "\t\t| Assignment 4.10 with vectors |" << std::endl
<< "\t\t|------------------------------|" << std::endl
<< "\t\t| 1) Add employees |" << std::endl
<< "\t\t| and their sales. |" << std::endl
<< "\t\t| 2) Show employees |" << std::endl
<< "\t\t| earnings |" << std::endl
<< "\t\t| 3) Show employees |" << std::endl
<< "\t\t| ranges of sales |" << std::endl
<< "\t\t| 4) Exit |" << std::endl
<< "\t\t'------------------------------'" << std::endl
<< "\t\t Selection: ";
std::cout.flush();
std::cin >> answer;
if(answer < 1 || answer > 4)
goto start_here;
reset_istream();
return answer;
}
void reset_istream(void)
{
if(std::cin.eof())
std::cin.clear();
else
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
void wait_for_enter(void)
{
std::cout << "press <enter> to continue...";
// Reset failstate, just in case.
std::cin.clear();
std::string line;
getline(std::cin, line);
}
For ease of access you can find the source code as a tar-ball.
If you have any questions feel free to ask, I'll try to explain everything uppon request.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:12 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|