User Input compared to Object array element

I posted the whole thing but what I really need help with is creating the printSalary function. He wants it to be able to go through the array and print names if they have an equal or higher salary but I have been getting errors when trying to compare user input up against an element in the array. Any help would be appreciated



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
#include <iostream>
#include<string>
#include<fstream>


class Employee {
private:
    int ID;
    string name;
    string depName;
    int salary;

public:
    Employee();

    void setID(int newID) { ID = newID; }
    void setName(string newName) { name = newName; }
    void setDepName(string newDepName) { depName = newDepName; }
    void setSalary(int newSalary) { salary = newSalary; }
    int getID() { return ID; }
    string getName() { return name; }
    string getDepName() { return depName; }
    int getSalary() { return salary; }
    void print(string* arr[], int n) {

        cout << "Employees" << endl;
        for (int i = 0; i < n; i++) {
            cout << ID << " " << name << " " << depName << " " << salary << endl;

        }
    }
    
};


using namespace std;


ifstream inFile;



int main() {

    int employees;
    int id;
    int salar;
    string names; 
    string dep; 

    //for menu choice later
    int num;
   
    inFile.open("employees.txt");

    inFile >> employees;
    Employee* eArr = new Employee[employees];

    for (int i = 0; i < employees; i++) {
        inFile >> id >> names >> dep >> salar;

        eArr[i].setID(id);
        eArr[i].setName(names);
        eArr[i].setDepName(dep);
        eArr[i].setSalary(salar);
    }

    inFile.close();

    cout << "1) Print Employees" << endl;
    cout << "2) Print by Department" << endl;
    cout << "3) Print by Salary" << endl;
    cout << "4) Exit" << endl; 



    return 0;

}

void printSalary(string* arr[]) {

    int salar2;
    cout << "Salary?";
    cin >> salar2;
    for (int i = 0; i < 0; i++) {
        if (arr[i].getSalary = salar2)

    }

}

Hello dwill129,

First it would be very helpful if you would provide the input file so that everyone will be using the same information to work with.

Line 54 defines a file stream and opens the file, but how do you know that it is open and ready to use? If you open a file for input it is a must that you check it before you continue with the program. Otherwise the program would be trying to read from a file stream that does not work.

On lines 24 and 81 you have (string* arr[]). So which is it a string pointer or an array? I have not seen this before, but I think it is safe to say that it would cause a compile error. Since you created a dynamic array you should stay with a pointer.

Have you learned about "vector"s yet? The program would be much easier with a vector than an array.

You use "new" to create a block of memory for your array, but I do not see anywhere where you use "delete" to free the memory when finished with it. If you use "new" there must be a matching "delete" before the program ends.

What you have right now is causing a memory leak which may or may not be freed when the program ends.

Nowhere in the program do I see where you call either "print" function.

When I was trying to understand the "printSalary" at the bottom I noticed if (arr[i].getSalary = salar2). "=" means assign and "==" means to compare. I believe you want the "==" here since an assignment here would not work.

The more I looked at the "printSalary" function the more I realized that the name is very misleading based on what the function is doing.

Andy
What the printSalary function aims to do is look into the array and print the names of whoever has the a higher amount(or equal to) than the salary gathered from the user input. As for the delete function I will put those In as the program is not yet complete and yes I have learned about vectors but he has specially asked for a dynamically allocated array. On lines 21 and 84 the string pointer array is meant to be to be an argument array passed to the prototype so that it knows which object array to print when the function is called. The reason both print function have yet to be called is because I haven't finished the menu portion of this program as I ran into problems when making the actual print functions to be used so there might be other problems besides the one I intially mentioned about trying to compare user input against a specific attribute in an element of the object array. And the file stream should be fine it made the array.
100
1 Kasimir Marketing 145000
2 Iona RD 37000
3 Iliana Production 141000
4 Gage Production 64000
5 Ira Marketing 121000
6 Cameron Purchasing 137000
7 Xander Purchasing 125000
8 Amal RD 72000
9 Lamar RD 125000
10 Daniel Marketing 59000
11 Petra RD 43000
12 Alec HR 57000
13 Lewis HR 94000
14 Lillian Purchasing 26000
15 Zeph Production 105000
16 Ciaran Purchasing 55000
17 Evan Purchasing 112000
18 Iola HR 73000
19 Jada Purchasing 147000
20 Elijah Purchasing 95000
21 Medge Purchasing 31000
22 Ian Marketing 134000
23 April Production 126000
24 Kiara Marketing 91000
25 Castor Purchasing 23000
26 Alana HR 79000
27 Lucy HR 65000
28 Farrah Purchasing 72000
29 Eagan Production 142000
30 Iola Marketing 67000
31 Jessica RD 33000
32 Eliana HR 74000
33 Donna Purchasing 120000
34 Alden RD 119000
35 Paul Marketing 141000
36 Hector Marketing 90000
37 Ignatius RD 26000
38 Allen RD 146000
39 Diana Production 132000
40 Willa Marketing 65000
41 Ulla Purchasing 54000
42 Alea RD 96000
43 Althea HR 143000
44 Elaine RD 98000
45 Kelly HR 29000
46 Karyn Marketing 71000
47 Hyatt RD 62000
48 Madonna Production 73000
49 Channing Marketing 38000
50 Darius HR 144000
51 Raya HR 23000
52 Lareina Production 86000
53 Sydney Marketing 82000
54 Benjamin RD 91000
55 Martin Marketing 79000
56 Beck Production 65000
57 Paula RD 118000
58 Grace Marketing 33000
59 Camille HR 140000
60 Briar Production 124000
61 Dorothy HR 28000
62 Aquila RD 149000
63 Prescott RD 125000
64 Dana RD 82000
65 Hedwig RD 53000
66 Mira HR 138000
67 Uma RD 61000
68 Ciara Marketing 98000
69 Yuri HR 139000
70 Hector Production 35000
71 Edan Production 44000
72 Callum Purchasing 65000
73 Phoebe Marketing 49000
74 Preston Purchasing 69000
75 Chanda RD 137000
76 Larissa RD 115000
77 Warren Marketing 116000
78 Orlando Marketing 124000
79 Sandra Marketing 57000
80 Kevyn HR 147000
81 Jasmine Purchasing 43000
82 Candice Purchasing 110000
83 Hanna HR 21000
84 Cheryl RD 101000
85 Jasmine HR 48000
86 Lisandra Purchasing 141000
87 Odysseus RD 53000
88 Marshall Marketing 100000
89 Amena Production 87000
90 Christian Purchasing 146000
91 Charles Marketing 44000
92 Julian RD 123000
93 Solomon HR 144000
94 Ava HR 142000
95 Jin HR 142000
96 Donovan Purchasing 110000
97 Chava Production 108000
98 Clark Marketing 107000
99 Jocelyn Marketing 24000
100 Leilani Marketing 105000
Topic archived. No new replies allowed.