Problem displaying strings from a struct using a pointer

Im working on an assignment where the user can enter some inputs via a struct pointer and display them. My problem is when I hit option 3 (lines 94 to 102) to display all records, the strings from the struct are not displayed. instead, blanks appear where the strings are supposed to. the float values display just fine. How come it displays the numeric variables, but not the string?

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

using namespace std;

struct employeeRec
{
    string employee_id;
    string last_name;
    string first_name;
    float hrs_worked;
    float pay_rate;
    float tax_rate;
};

int main()
{
    bool again = true;
    int recorded = 0, remaining = 20;

    while (again)
    {
        const int max = 20;
        int written, option;
        employeeRec *p, records[max];

        p = &records[0];

        cout << "\n1 - Enter new employee(s)\n"
             << "2 - Find one employee record\n"
             << "3 - Display all employee records\n"
             << "4 - Quit\n\n";

        cout << "Enter an option: ";
        cin >> option;

        switch (option)
        {
            case 1:
            {
                cout << "\nHow many records do you wish to enter? (Max total of 20 records): ";
                cin >> written;

                cin.ignore();

                if (written < 0)
                {
                    written = written * -1;
                }

                for (int n = 0; n < written; n++)
                {
                    cout << "\nRecord #" << n << "\n";
                    cout << "1 of 6 - Enter Employee ID: ";
                    getline(cin, p -> employee_id);

                    cout << "\n2 of 6 - Enter Employee Last Name: ";
                    getline(cin, p -> last_name);

                    cout << "\n3 of 6 - Enter Employee First Name: ";
                    getline(cin, p -> first_name);

                    cout << "\n4 of 6 - Enter Hours Worked: ";
                    cin >> p -> hrs_worked;

                    cout << "\n5 of 6 - Enter Pay Rate: ";
                    cin >> p -> pay_rate;

                    cout << "\n5 of 6 - Enter Pay Rate: ";
                    cin >>p -> tax_rate;

                    cin.ignore();

                    cout << "\nEmployee Recorded\n";

                    p++;
                }

                recorded = recorded + written;
                remaining = remaining - recorded;

                break;
            }

            case 2:
            {
               cout << "\nFind an employee option";
               break;
            }

            case 3:
            {
                for (int n = 0; n < written; n++)
                {
                    cout << "\n\nEmployee ID: " << p -> employee_id << "\n"
                         << "Last Name: " << p -> last_name << "\tFirst Name: " << p -> first_name << "\n"
                         << "Hours Worked: " << p -> hrs_worked << "\tPayrate: " << p -> pay_rate << "\tTax Rate: " << p -> tax_rate << "\n\n";
                    p++;
                }
                break;
            }

            case 4:
            {
                cout << "\nQuitting...";
                again = false;
                break;
            }

            default:
            {
                cout << "Not a valid option. Enter a valid option.\n";
                break;
            }
        }
    }
}
Last edited on
employeeRec *p, records[max];
I suspect you wanted records to be an array of employeeRec objects but, since you've declared it on the same line as p, employeeRec is actually an array of pointers to employeeRec.
Also the C-style array collapses to a pointer you don't need two separate variables, p and records, to practice pointer arithmetic. you can use the name of the array as the pointer.
Finally you are allocating additional memory than you might actually need, so it'd be more efficient to allocate the array dynamically and delete it after use
Topic archived. No new replies allowed.