Lab 07: Employee Database

Part 1: basic functionality

Download the data file and add it to your project. Look at the contents so that you understand what data you'll be working with.

employees.txt

(a) In main, declare five arrays to hold the employee data: names, titles, review1, review2 and review3.

(Don't forget to also declare an int to keep count of the number of employees stored in the arrays!)

(b) Also in main, read in the employees from the file and store their data in the arrays. For each line in the file, you're going to add a value to each of the arrays and update the counter.

It's hard to see if you're doing it right without printing out the arrays. Before you do that though, use the debugger by setting a breakpoint after you're done reading in. When the code breaks at that point (get it?), you can see the values in your arrays in the Autos pane in Visual Studio (names differ in other environments).

If you're having trouble, consider dropping a breakpoint inside your file reading loop to step through how you're changing the arrays as you go.

(c) Write a function that takes an employee index and prints their information to the screen like so (name and title):

Kim (Boss)

The index is the position in the arrays. Schweller is index 0, for example, Wylie is index 1, and Kim is index 2. We're getting more advanced here, so when I say that the function "take's an employee's index", that doesn't mean that the index is the only thing it takes. You'll need to pass in other things as well.

Test your function by calling it with a variety of indeces in main.

(d) Write a function to print all the employees, using the function you just wrote to print one employee.

Test your function by...well, printing all the employees.

Part 2: building up usefulness

(a) Write a function that takes an employee name and returns their index.

To test this function, you will use what we call a unit test in main. It's a bit of code that checks if a function returns what you expect, like this:

	int test_index;
	
	test_index = findEmployee("Schweller", names, employee_ct);
	if (test_index == 0) {
	   cout << "findEmployee test (Schweller) passed" << endl;
	} else {
	   cout << "findEmployee test (Schweller) failed, expected 0, got " << test_index << endl;
	}

      	test_index = findEmployee("Yu", names, employee_ct);
	if (test_index == 5) {
	   cout << "findEmployee test (Yu) passed" << endl;
	} else {
	   cout << "findEmployee test (Yu) failed, expected 5, got " << test_index << endl;
	}
      

As you can see, the code for those two tests is highly redundant. That's always a sign that you could write other functions to further simplify and automate the testing process.

(b) Write a function that takes an employee index and returns their average review score (over the three review scores).

Create similar unit tests to verify this function as well - remember to test more than one case.

(c) Write a function that takes an employee name and prints out their information like this (don't worry about how many decimal places it prints):

	Kim (Boss)
	Average review score: 77.3333
      

Don't write redundant, untested code! You already wrote and tested functions to print an employee's name and title, find them by name and calculate their average review score. Use them!

Test your function by calling it with a variety of names in main. It's hard to unit test output-to-the-screen type functions, so don't worry about that right now.

Part 3: higher level operations

Keep building up functionality on top of what you already have!

(a) Write a function that returns the index of the employee with the highest average review score.

Test your function by printing the employee and their score.

(b) Write a function that takes an employee name and a new title for them and updates the appropriate array.

Test your function by changing a few titles, then printing all the employees.