-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsalaries_test.cpp
More file actions
139 lines (127 loc) · 3.21 KB
/
salaries_test.cpp
File metadata and controls
139 lines (127 loc) · 3.21 KB
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
// Company Salaries
// Jason Hsu
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <stdlib.h>
#include <fstream> //read file as input
using namespace std;
bool readFile(int &count);
void highsalary(int count);
double totalsalary(int count);
struct employee
{
string firstname;
string lastname;
int salary;
};
employee *allemployee = NULL;
int main()
{
bool isGoodData = false;
cout << "Welcome to the employee program." << endl;
int count=0;
while (!isGoodData)
isGoodData = readFile(count);
cout << "(1) Find employee with highest salary" << endl;
cout << "(2) Find employee with lowest salary" << endl;
cout << "(3) Calculate average salary" << endl;
cout << "(4) Calculate total salary" << endl;
cout << "(5) Exit" << endl;
int ans;
highsalary(count);
double tsalary = totalsalary(count);
cout << "Enter a choice: ";
cin >> ans;
while (ans != 5)
{
switch (ans)
{
case 1: cout << allemployee[count - 1].firstname << " " << allemployee[count - 1].lastname << " has the highest salary of " << allemployee[count - 1].salary << "." << endl;
break;
case 2: cout << allemployee[0].firstname << " " << allemployee[0].lastname << " has the lowest salary of " << allemployee[0].salary << "." << endl;
break;
case 3: cout << "The average salary is " << tsalary / count << "." << endl;
break;
case 4: cout << "The total salary is " << tsalary << "." << endl;
break;
}
cout << "Enter a choice: ";
cin >> ans;
}
if (ans == 5 ) {
cout << "Thank you for using the employee program." << endl;
return 0;
}
}
//readFile function; re-prompts user to input if input is invalid
bool readFile(int &count)
{
char filename[100];
ifstream fin;
cout << "Enter the input file: ";
cin >> filename; //no longer limits the input to be input.txt only
fin.open(filename);
if (fin.fail())
{
cout << "Failed to open file. " << endl;
return false;
}
char buf[100];
string input;
getline(fin, input);
while (input != "" && !fin.fail())
{
getline(fin, input);
count++;
}
if (allemployee != NULL) //check if bad input is in memory
delete[] allemployee;
allemployee = new employee[count];
fin.close();
fin.open(filename);
int i=0;
while (!fin.fail())
{
if (!(fin >> allemployee[i].firstname >> allemployee[i].lastname >> allemployee[i].salary))
{
cout << "ERROR -- BAD DATA" << endl;
return false;
}
fin.ignore(100, '\n');
i++;
}
return true;
}
void highsalary(int count) //bubblesort the salaries
{
for (int i = 0; i < count; i++)
{
for (int j = 0; j < count - 1; j++)
{
if (allemployee[j].salary > allemployee[j + 1].salary)
{
int temp = allemployee[j].salary;
allemployee[j].salary = allemployee[j + 1].salary;
allemployee[j + 1].salary = temp;
string tempName = allemployee[j].firstname;
allemployee[j].firstname = allemployee[j + 1].firstname;
allemployee[j + 1].firstname = tempName;
tempName = allemployee[j].lastname;
allemployee[j].lastname = allemployee[j + 1].lastname;
allemployee[j + 1].lastname = tempName;
}
}
}
}
double totalsalary(int count)
{
double totalsalary=0;
for (int i=0; i < count; i++)
{
totalsalary += allemployee[i].salary;
}
return totalsalary;
}