What are you having trouble with? The sorting of the salaries in ascending order? Calculating the tax for each salary? Using the struct?
You have multiple questions in there and only a very basic code example that doesn't show what you have done.
Basically you just figure out each salary after tax (Store that value in a separate variable in the struct), after you have figured out that make a function or just some code that sorts each employee by their salary after taxes in a vector.
1 2 3 4 5 6 7 8 9
// New struct to hold employee info
struct Employee
{
string name; // Use string instead of char arrays they are much easier to work with
double salary; // I think double would be better for this.
string birthday; // String is better suited for this since it can support MM/DD/YYYY format
string sex; // Again string is better, even better would be a enum
double salaryAfterTax; // New variable to hold salary after taxes;
};
Store all the employee's in a vector<Employee> employees; or some other container.
Then when you want to sort them you can just sort each employee by their salaries with a common sorting algorithm (Bubblesort is what most beginners use though it is not the best) and sort them by their salaries in ascending order.
Sorry I couldn't be much more help but you didn't really give us a exact thing that you were having trouble with or any code to go off of. If you are confused about anything I posted just let me know and I will try to help explain it more.
My advice would be to use a vector (Or Array if you really want to) to store each employee in. After you have got all the info for each employee and stored them in the container you can use the sort function in the algorithm header to sort them for you.
The sort() function takes 2 arguments, a iterator to the first element and a iterator to the last element to sort (Meaning it sorts everything between them two iterators). It also takes a third optional argument that is used to define what you want to sort (See my example below).
Here is a quick example of how to sort a struct by salary using the sort function.
#include <iostream>
#include <string>
#include <vector> // Need this is you want to use vector<type>
#include <algorithm> // Need this to use sort()
usingnamespace std;
// Simple struct with two members
struct Employee
{
string name;
double salary;
};
// We need this to tell the sort() function that we want to sort it by
// our salary member or our struct.
bool mySortFunction(const Employee &one, const Employee &two)
{
return (one.salary < two.salary);
}
int main()
{
// This holds all employees
vector<Employee> employees;
// This enters all the info for each employee and stores it in the vector
for (unsigned i = 0; i != 5; ++i)
{
Employee temp;
cout << "Enter Name: ";
cin >> temp.name;
cout << "Enter Salary: ";
cin >> temp.salary;
employees.push_back(temp);
}
// Takes care of sorting the vector by salaries. Notice how we used the 3rd
// optional argument by passing our mySortFunction to it. This tells the
// sort() function that we want to sort by salary.
sort(employees.begin(), employees.end(), mySortFunction);
// Prints each employee in the vector. Since the vector has already been sorted
// by salary it will be in ascending order.
for (vector<Employee>::iterator i = employees.begin(); i != employees.end(); ++i)
{
cout << "Name: " << i->name << " " << "Salary: " << i->salary << endl;
}
return 0;
}
The key part of this is the sort function and how we used our own function we made to determine how to sort it. I would highly suggest you look up how the sort() function works so you fully understand it. The sort() function also works on arrays if you need to use them.
Hope this helps you with understanding how sorting works in C++ and if you have any questions just let me know and I will be glad to help you out.
Thank you much for listening to me
In fact, I do not understand this much
I just want to make my program as I have told you the same instructions
Because this is exactly required of me in my program
I can not understand very well help me
Thank you a lot
If there is help to be easier than this, I will be grateful to you
Don't think I can help any more then that really. That is one of the easiest way of sorting of a struct that I can think of. All others would require you to right your own sorting algorithm.
My advice would be just to test out what I posted and mess around with it. Also research any of the keywords that you don't understand. Just go down the program and try and figure out what each thing does, when you run into something you don't understand google it and try and figure out what it does.