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;
}
|