help with my assignment
Apr 4, 2013 at 6:37pm UTC
when calc number employees at lines 119 and 130
The program gave me that in output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
#include<iostream>
#include <vector>
struct employee //ahmed.ha.hnd
{
char name[20];
float salary;
int birthday;
char sex [10];
};
int main()
{
employee emp[100];
{
int n;
std::cout << "A program for collecting employee information" ;
std::cout << std::endl;
std::cout << "And displaying the collected information" ;
std::cout << std::endl << std::endl;
std::cout << "How many employees:" ;
std::cin >> n;
std::cout << std::endl;
std::cout << "Enter the following information:" <<std::endl;
std::cout << std::endl;
for (int i=0; i<n; i++)
{
std::cout << "Enter information for employee no: " << i;
std::cout << std::endl;
std::cout<<"Enter the Employee name :" ;std::cin>>emp[i].name;
std::cout<<"Enter the salary :" ;std::cin>>emp[i].salary;
std::cout<<"Enter the birthday :" ;std::cin>>emp[i].birthday ;
std::cout<<"Enter the sex :" ;std::cin>>emp[i].sex;
std::cout<<std::endl;
}
{
employee temp;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (emp[i].salary<emp[j].salary)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
std::cout << "Employee entered information:" << std::endl;
std::cout << "============================" << std::endl;
std::cout << "Name salary birthday Sex " << std::endl;
for (i = 0; i < n; i++)
{
std::cout << emp[i].name << "\t" ;
std::cout << emp[i].salary << "\t" ;
std::cout << emp[i].birthday; std::cout << "\t" ;
std::cout << emp[i].sex ;
std::cout << std::endl;
}
int highest_salary=0;
int total_salary=0;
for (i = 0; i < n; i++)
{
if (emp[i].salary > highest_salary)
{
highest_salary = emp[i].salary;
}
total_salary += emp[i].salary;
}
std::cout<<std::endl;
std::cout << "Total Salary: " << total_salary <<std::endl;
std::cout << "============" <<std::endl;
std::cout << "Highest Salary: " << highest_salary << std::endl;
std::cout << "==============" <<std::endl;
float tax=0;
for (i = 0; i < n; i++)
{
if (emp[i].salary <= 1999)
{
tax = (emp[i].salary*5)/100.0;
}
else if (emp[i].salary<=2999)
{
tax=(emp[i].salary*7.5)/100.0;
}
else if (emp[i].salary<=3999)
{
tax=(emp[i].salary*10)/100.0;
}
else if (emp[i].salary>4000)
{
tax=(emp[i].salary*15)/100.0;
}
emp[i].salary -= tax;
}
std::cout<<"Salary after tax :" <<" Employee Name : " <<std::endl;
std::cout<<"================ " <<" =============" <<std::endl;
for (i = 0; i < n; i++)
std::cout << emp[i].salary<<" " << emp[i].name << std::endl;
}
int number=1;
if (emp[i].salary>=15000)
{ //Number of employees who salary more than 15000
number++;
}
std::cout<<"Number of employees who salary more than 15000 :" <<std::endl;
std::cout<<number<<std::endl;;
}
}
output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
A program for collecting employee information
And displaying the collected information
How many employees:3
Enter the following information:
Enter information for employee no: 0
Enter the Employee name :asdf
Enter the salary :15000
Enter the birthday :1987
Enter the sex :m
Enter information for employee no: 1
Enter the Employee name :asra
Enter the salary :5000
Enter the birthday :1966
Enter the sex :f
Enter information for employee no: 2
Enter the Employee name :aswar
Enter the salary :15000
Enter the birthday :1984
Enter the sex :m
Employee entered information:
============================
Name salary birthday Sex
asdf 15000 1987 m
aswar 15000 1984 m
asra 5000 1966 f
Total Salary: 35000
============
Highest Salary: 15000
==============
Salary after tax : Employee Name :
================ =============
12750 asdf
12750 aswar
4250 asra
Number of employees who salary more than 15000 :
1
Press any key to continue
Last edited on Apr 4, 2013 at 7:50pm UTC
Apr 4, 2013 at 8:03pm UTC
Apr 4, 2013 at 8:28pm UTC
I am guessing you mean it is suppose to be 2 instead of 1? Not sure because not sure if you want before tax or after.
But if it is suppose to be 2 and not 1 the reason is this.
1 2 3 4 5 6 7
int number=1;
if (emp[i].salary>=15000)
{ //Number of employees who salary more than 15000
number++;
}
You are just checking only 1 person since you don't have the if statement in a loop that runs through the container and checks that against each employee.
So put that into a loop like this
1 2 3 4 5 6 7
int number = 0;
for (size_t i = 0; i != 100; ++i)
{
if (emp[i].salary>=15000)
number++;
}
The 100 referes to your array size, which I would change to a variable if I were you that way it is easier to maintain.
Example
const unsigned SIZE = 100;
employee emp[SIZE];
Or just change it to a vector and it would be even better ;p
Apr 4, 2013 at 8:54pm UTC
yes i want that befor tax ,
do you main this in my code , but gave me error
and// the size in my code is (n)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#include<iostream>
#include <vector>
struct employee //ahmed.ha.hnd
{
char name[20];
float salary;
int birthday;
char sex [10];
};
int main()
{
// employee emp[100];
const unsigned SIZE = 100;
employee emp[SIZE];
{
int n;
std::cout << "A program for collecting employee information" ;
std::cout << std::endl;
std::cout << "And displaying the collected information" ;
std::cout << std::endl << std::endl;
std::cout << "How many employees:" ;
std::cin >> n;
std::cout << std::endl;
std::cout << "Enter the following information:" <<std::endl;
std::cout << std::endl;
for (int i=0; i<n; i++)
{
std::cout << "Enter information for employee no: " << i;
std::cout << std::endl;
std::cout<<"Enter the Employee name :" ;std::cin>>emp[i].name;
std::cout<<"Enter the salary :" ;std::cin>>emp[i].salary;
std::cout<<"Enter the birthday :" ;std::cin>>emp[i].birthday ;
std::cout<<"Enter the sex :" ;std::cin>>emp[i].sex;
std::cout<<std::endl;
}
{
employee temp;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (emp[i].salary<emp[j].salary)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
std::cout << "Employee entered information:" << std::endl;
std::cout << "============================" << std::endl;
std::cout << "Name salary birthday Sex " << std::endl;
for (i = 0; i < n; i++)
{
std::cout << emp[i].name << "\t" ;
std::cout << emp[i].salary << "\t" ;
std::cout << emp[i].birthday; std::cout << "\t" ;
std::cout << emp[i].sex ;
std::cout << std::endl;
}
int highest_salary=0;
int total_salary=0;
for (i = 0; i < n; i++)
{
if (emp[i].salary > highest_salary)
{
highest_salary = emp[i].salary;
}
total_salary += emp[i].salary;
}
std::cout<<std::endl;
std::cout << "Total Salary: " << total_salary <<std::endl;
std::cout << "============" <<std::endl;
std::cout << "Highest Salary: " << highest_salary << std::endl;
std::cout << "==============" <<std::endl;
float tax=0;
for (i = 0; i < n; i++)
{
if (emp[i].salary <= 1999)
{
tax = (emp[i].salary*5)/100.0;
}
else if (emp[i].salary<=2999)
{
tax=(emp[i].salary*7.5)/100.0;
}
else if (emp[i].salary<=3999)
{
tax=(emp[i].salary*10)/100.0;
}
else if (emp[i].salary>4000)
{
tax=(emp[i].salary*15)/100.0;
}
emp[i].salary -= tax;
}
std::cout<<"Salary after tax :" <<" Employee Name : " <<std::endl;
std::cout<<"================ " <<" =============" <<std::endl;
for (i = 0; i < n; i++)
std::cout << emp[i].salary<<" " << emp[i].name << std::endl;
int number = 0;
for (size_t i = 0; i != 100; ++i)
{
if (emp[i].salary>=15000)
number++;
}
}
}
}
Last edited on Apr 4, 2013 at 8:54pm UTC
Apr 4, 2013 at 9:23pm UTC
also look what happen
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#include<iostream>
#include <vector>
struct employee //ahmed.ha.hnd
{
char name[20];
float salary;
int birthday;
char sex [10];
};
int main()
{
//employee emp[100];
const unsigned SIZE = 100;
employee emp[SIZE];
{
int n;
std::cout << "A program for collecting employee information" ;
std::cout << std::endl;
std::cout << "And displaying the collected information" ;
std::cout << std::endl << std::endl;
std::cout << "How many employees:" ;
std::cin >> n;
std::cout << std::endl;
std::cout << "Enter the following information:" <<std::endl;
std::cout << std::endl;
for (int i=0; i<n; i++)
{
std::cout << "Enter information for employee no: " << i;
std::cout << std::endl;
std::cout<<"Enter the Employee name :" ;std::cin>>emp[i].name;
std::cout<<"Enter the salary :" ;std::cin>>emp[i].salary;
std::cout<<"Enter the birthday :" ;std::cin>>emp[i].birthday ;
std::cout<<"Enter the sex :" ;std::cin>>emp[i].sex;
std::cout<<std::endl;
}
{
employee temp;
for (int i = 0; i < n; i++)
{
for (int j = i+1; j < n; j++)
{
if (emp[i].salary<emp[j].salary)
{
temp=emp[i];
emp[i]=emp[j];
emp[j]=temp;
}
}
}
std::cout << "Employee entered information:" << std::endl;
std::cout << "============================" << std::endl;
std::cout << "Name salary birthday Sex " << std::endl;
for (i = 0; i < n; i++)
{
std::cout << emp[i].name << "\t" ;
std::cout << emp[i].salary << "\t" ;
std::cout << emp[i].birthday; std::cout << "\t" ;
std::cout << emp[i].sex ;
std::cout << std::endl;
}
int highest_salary=0;
int total_salary=0;
for (i = 0; i < n; i++)
{
if (emp[i].salary > highest_salary)
{
highest_salary = emp[i].salary;
}
total_salary += emp[i].salary;
}
std::cout<<std::endl;
std::cout << "Total Salary: " << total_salary <<std::endl;
std::cout << "============" <<std::endl;
std::cout << "Highest Salary: " << highest_salary << std::endl;
std::cout << "==============" <<std::endl;
float tax=0;
for (i = 0; i < n; i++)
{
if (emp[i].salary <= 1999)
{
tax = (emp[i].salary*5)/100.0;
}
else if (emp[i].salary<=2999)
{
tax=(emp[i].salary*7.5)/100.0;
}
else if (emp[i].salary<=3999)
{
tax=(emp[i].salary*10)/100.0;
}
else if (emp[i].salary>4000)
{
tax=(emp[i].salary*15)/100.0;
}
emp[i].salary -= tax;
}
std::cout<<"Salary after tax :" <<" Employee Name : " <<std::endl;
std::cout<<"================ " <<" =============" <<std::endl;
for (i = 0; i < n; i++)
std::cout << emp[i].salary<<" " << emp[i].name << std::endl;
int number = 0;
for ( i = 0; i != 100; ++i)
{
if (emp[i].salary>=15000)
{
number++;
}
}
std::cout<<"Number of employees who salary more than 15000" <<std::endl;
std::cout<<number<<std::endl;
}
}
}
//std::cout<<"Number of employees who salary more than 15000"<<std::endl
//std::cout<<number<<std:endl;
output :
dont calc the number 2 from employees
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
How many employees:3
Enter the following information:
Enter information for employee no: 0
Enter the Employee name :aa
Enter the salary :15000
Enter the birthday :
5
Enter the sex :m
Enter information for employee no: 1
Enter the Employee name :dd
Enter the salary :1000
Enter the birthday :8
Enter the sex :m
Enter information for employee no: 2
Enter the Employee name :rr
Enter the salary :15000
Enter the birthday :5
Enter the sex :m
Employee entered information:
============================
Name salary birthday Sex
aa 15000 5 m
rr 15000 5 m
dd 1000 8 m
Total Salary: 31000
============
Highest Salary: 15000
==============
Salary after tax : Employee Name :
================ =============
12750 aa
12750 rr
950 dd
Number of employees who salary more than 15000
0
Press any key to continue
Apr 5, 2013 at 12:04am UTC
1 2
Thanks
I found the problem.
Last edited on Apr 5, 2013 at 1:36am UTC
Topic archived. No new replies allowed.