Array of objects - CODe Mode

Pages: 12
Oh no please do not say that...I am still sitting here trying to do this. Going on 36 hours and I know this is probably extremely easy for you.
Haha. Yeah it is extremely easy for me. But why? Cus I spent the last 6 months learning as much as I can about C++ as fast as I can. And I didn't learn any of it by asking questions in forums. I researched and read.

Give me thirty minutes and I'll try to help you further.
O wow, it has been about 3 for me and taking in from school books and on my own as much as I can, you may be more prone to being a programmer :P.

Okay sounds good, thank you.
Lol! I didn't mean give me thirty minutes to make the program. I wanted to eat :3
LOL! I knew that silly man.
This is what I have:

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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>

using namespace std;

//////////////
// Employee //
//////////////

class Employee
{

public:
    void setData ( string, string, int, long );
    void displayData ( );

    int getAge ( );

private:
    string employeeName;
    string employeeTitle;
    int employeeAge;
    long employeeSalary;
    // I wouldn't put employee on the beginning, but since you have the class
    //   in the same file as everything, we shall leave it like this.
};

void Employee::setData ( string Name, string Title, int Age, long Salary )
{
    // I wouldn't use a setData function. But since we aren't using pointers
    //   this will do for the scope of this project.

    employeeName = Name;
    employeeTitle = Title;
    employeeAge = Age;
    employeeSalary = Salary;
}

void Employee::displayData ( )
{
    cout << left << setw ( 20 ) << employeeName << setw ( 20 ) << employeeTitle << setw ( 20 ) << employeeAge << setw ( 20 ) << employeeSalary << endl;
}

int Employee::getAge ( )
{
    return employeeAge;
}

/////////////////////////
// Function Prototypes //
/////////////////////////

template <typename Number> Number stringToNumber ( string & String )
{
    stringstream Stream ( String );
    Number Result;
    return ( Stream >> Result ) ? Result : 0;
}
// This is a template function. It converts a string to a number.
// Template functions must be implemented with its declaration.

///////////////////
// Main Function //
///////////////////

int main ( )
{
    Employee employees[100];
    int totalEmployees = 0;

    string userInput;
    // Make a string to hold user input.
    // This allows us to do checking and stuff.

    string ename, etitle;
    int eage;
    long esalary;
    char again;

    cout << setprecision ( 2 ) << setiosflags ( ios::fixed ) << setiosflags ( ios::showpoint );
    // No sense in doing this every time.

    do
    {
        totalEmployees++;

        cout << "Please enter employee name: ";
        cin >> userInput;
        ename = userInput;
        cout << endl;

        cout << "Please enter employee title: ";
        cin >> userInput;
        etitle = userInput;
        cout << endl;

        cout << "Please enter employee age: ";
        cin >> userInput;
        eage = stringToNumber <int> ( userInput );
        // This is how we use the template function.
        // We want it to turn our string into an int, so we put int
        //   inside < >, which is between the function name and ( )

        cout << endl;

        cout << "Please enter employee salary: ";
        cin >> userInput;
        esalary = stringToNumber <long> ( userInput );

        cout << endl;

        employees[totalEmployees - 1].setData ( ename, etitle, eage, esalary );
        // We can cheat this way and use totalEmployees - 1 to get the
        //   first employee object in our array.

        cout << "Next Employee? Type n or N to stop the data entry. ";
        cin.ignore ();
        getline ( cin, userInput );
        // Notice that we use getline here because getline includes the newline
        //   in the data. Which is what we need to move on if the user enters
        //   nothing.

        if ( tolower ( userInput[0] ) == 'n' )
        {
            // Now we check if the first letter is n or N
            // The tolower function  takes a char and makes it the
            //   lowercase equivalent
            // Notice that userInput[0] gives us the first letter in
            //   the string'
            userInput = "exit";
        }
        else
        {
            userInput = "";
        }

        cout << endl;
    }
    while ( userInput != "exit" );

    cout << "\t\tEmployee Statistical Report:" << endl;
    cout << endl;

    cout << left << setw ( 20 ) << "Employee Name" << left << setw ( 20 ) << "Employee Title" << left << setw ( 20 ) << "Age" << setw ( 20 ) << "Salary" << endl;

    int totalAge = 0;
    int averageAge = 0;
    for ( unsigned int index = 0; index < totalEmployees; index++ )
    {
        employees[index].displayData ( );
        totalAge += employees[index].getAge ( );
    }
    averageAge = totalAge / totalEmployees;
    cout << "Average Age is " << averageAge;

    cout << endl;
    cout << endl;

    cout << "Press enter to exit.";
    cin.ignore ( );
    getline ( cin, userInput );
    // Notice that we use the getline here also.
    // The cin.ignore() is used to clear a single newline that we don't
    //   want in there.

    return 0;
}
O wow I am trying to figure out all this now...but when I am running the program if we enter a full name so, name and last name, the title takes this in and skips to age.
http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/

That's because 'cin >>' gets an array of characters (a string is treated like an array of characters) until it reaches a whitespace (space, newline, null character) and puts it into the variable on the right.

Because of this:
cin >> FirstName >> LastName;
is a valid way to retrieve two strings from the input.

However, in our code, it takes the first name, and doesn't expect any spaces. You will have to come up with a way to do this.
There are many many ways to do input checking. Try googling it.
Okay, thank you so much boy u really helped me out here, but definitely analyzing this code now to learn exactly what is going on. At least trying to, your method is showing me a lot of new ways, you are good.
Thanks. If I can find some code I made before for user input checking, then I'll post it here. Otherwise, good luck!
Again thanks that would be nice, also if there were other tutorials that you found out there that you felt aid in a beginners experience feel free to post or pm me whenever you want :).

Hmm. Well. To be honest, I don't use tutorials that much. For the most part, I just type stuff into google and click all the links on the first page, then read what they have for me. And so on. If you read a whole lot of stuff, and keep reading, things will eventually click. Then when you can start naming things that you need help with or don't understand, you can google the name and read specifically on those subjects.

If you have any questions that you just can't find answers to, then asking someone or posting your question on a forum is the best way to go. You may get flamed for not searching enough, or other stuff, but there will likely be someone who gives you a hint or clue to look for.
Topic archived. No new replies allowed.
Pages: 12