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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <stdexcept>
using namespace std;
//Class
class Employee
{
private:
string fname[5], lname[5];
string gender[5], dependents[5], anSal[5];
char gen[5];
int dep[5];
int x;
double anSalary[5], wkSal[5];
int title, empCount;
public:
// Constructors
Employee()
{
fname[x] = "Not Given";
lname[x] = "Not Given";
gen[x] = 'U';
dep[x] = 0;
anSalary[x] = 20000;
}
Employee(string fname[], string lname[], string gender[], string dependents[], string anSal[])
{
getInfo();
for (int x = 0; x <= empCount; x++)
{
fname[x] = fname[x];
lname[x] = lname[x];
string gndr = gender[x];
string dpnt = dependents[x];
gen[x] = ConvertGender(gndr);
dep[x] = ConvertStringtoInt(dpnt);
anSalary[x] = ConvertStringtoDouble(anSal[x]);
}//end for
calcWkSal();
wkSal[x] = wkSal[x];
}//end employee constructor
// Member Functions
//Info::Input
void Employee::getInfo();//Input
void Employee::calcWkSal();//Processing
//Conversions::Processing
char Employee::ConvertGender(string gndr);
int Employee::ConvertStringtoInt(string &dpnt);
double Employee::ConvertStringtoDouble(string anSal);
//Printing::Output
void Employee::print();
void Employee::displayDivider();
};
//Inputs & Calculations
void Employee::getInfo()//Good to go, errors gone!
{
bool loop = true;
int answer = 0;
empCount = 0;
cout << "Do you wish to add Employee information? 0 to quit, 1 to add.\t";
cin >> answer;
//Check
if (answer == 0)
{
loop = false;
}
else if (answer == 1)
{
loop = true;
}
else
{
while ((answer != 1) || (answer != 0))//check for correct entries
{
cout << "\nError: Answer must be a number." << endl;
cout << "Enter 1 to add, 0 to quit.\t";
cin >> answer;
}//end while loop
}
while (loop == true)
{
empCount++;
if (empCount == 5)
{
cout << "\nError: Too many employees.\n";
loop = false;
return;
}
else
{
loop = true;
}
title = 1;
displayDivider();
//first name
cout << "\nEnter First Name:\t";
cin >> fname[empCount];
//last name
cout << "\nEnter Last Name:\t";
cin >> lname[empCount];
//gender
cout << "\nEnter Gender:\t\t";
cin >> gender[empCount];
while ((gender[empCount] != ("female")) && (gender[empCount] != ("male")) && (gender[empCount] != ("Female")) && (gender[empCount] != ("Male")))
{
cout << "Error: Gender must be Male or Female.\n";
cout << "Enter Gender:\t\t";
cin >> gender[empCount];
}
//Dependents
cout << "\nEnter Dependents:\t";
cin >> dependents[empCount];
//Salary
cout << "\nEnter Annual Salary:\t";
cin >> anSal[empCount];
cout << "\n" << endl;
title = 0;
displayDivider();//end divider
//check to continue
cout << "Would you like to add Employee Information? 1 to add, 0 to quit:\t";
cin >> answer;
switch (answer)
{
case 0: loop = false; return;
case 1: loop = true; break;
default:
while ((answer != 0) && (answer != 1))//check for correct entries
{
cout << "\nError: Answer must be a number." << endl;
cout << "Enter 1 to add, 0 to quit.\t";
cin >> answer;
}//end while loop
}//end switch case for quit or continue
}//end while loop
return;
}//end get info
void Employee::calcWkSal()
{
for (int i = 0; i <= empCount; i++)//runs every employee through and stores the values
{
wkSal[i] = anSalary[i] / 52;
} //end for
}//end weekly salary calculations
//Conversions
char Employee::ConvertGender(string gndr)
{
if (gndr[0] == 'F' || gndr[0] == 'f')
{
return 'F';
}
else
{
return 'M';
}
}
int Employee::ConvertStringtoInt(string &dpnt)
{
int value = 0;
for (int i = 0; i < dpnt.length(); i++)
{
value *= 10;
// we multiply value by 10 to move the digits one base 10 position
value += dpnt[i] % 48;
//converts from ascii to integer value
value = (round(value));//rounds value to nearest integer
}
return value;
}
double Employee::ConvertStringtoDouble(string anSal)
{
double value = 0.0;
for (int i = 0; i < anSal.length(); i++)
{
value = atof(anSal.c_str());
}
return value;
}
//Output
void Employee::print()
{
cout << "\n\nEmployee Information:\n";
for (int i = 0; i <= empCount; i++)
{
title = 0;
displayDivider();
cout << "Name:\t" << fname[i] << " " << lname[i] << endl;
cout << "Gender:\t" << gen[i] << endl;
cout << "Dependents:\t" << dep[i] << endl;
cout << "Annual Salary:\t" << anSalary[i] << endl;
cout << "Weekly Salary:\t" << wkSal[i] << endl;
}//end for
title = 0;
displayDivider();
}//end print
void Employee::displayDivider()
{
if (title == 1)
{
cout << "\n--------------------- Employee " << empCount << " ---------------------\n";
return;
}
else if (title == 0)
{
cout << "------------------------------------------------------\n";
}//end ifs
}//end dividers
int main(string fname[], string lname[], string gender[], string dependents[], string anSal[])
{
Employee client(fname, lname, gender, dependents, anSal);
client.print();
}
|