how can order by asc salary in c++

How can I arrange Salaries Ascending
The show salaries after tax
To be applied with the struct
anyone know a good reference ?


example, my prgram
1
2
3
4
5
6
7
8
9
10
11
12
13
 struct employee
{
char name[20];
int salary;
int birthday;
char sex [6];

};
void main()
{
struct employee emp[25];
for(int i=0;i<3;i++)
{...........
Last edited on
closed account (3qX21hU5)
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.
________________________________________
Last edited on
------------------------------------
Last edited on
please any one help me
closed account (3qX21hU5)
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.

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
#include <iostream>
#include <string>
#include <vector>     // Need this is you want to use vector<type>
#include <algorithm> // Need this to use sort()

using namespace 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.
Last edited on
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
closed account (3qX21hU5)
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.

Sorry I couldn't be of more help.
ya
i will try
I will try to understand
Thank you a lot
Topic archived. No new replies allowed.