I was going about this in the wrong fashion. Thanks for the help anyways!! Here is the final code on what I was trying at! :P
Code:
/******************************************************
** Name: Joshua Long
** Filename: CPP_FINAL1.cpp
** Project Description: Enter the number of employees you want
to input into the database, enter all of the employees and
then sort the employees by last name. Prompt user to search
for an employee by last name then display employee.
** Output: Employee data that was entered and then organized
by the program to have better formatting.
** Input: Employee data
** Strees Level: Very HIGH ;)
******************************************************/
// Include Files
#include <iostream> // used for cin, cout
#include <iomanip>
#include <string.h>
#include <stdio.h>
using namespace std;
const int MAX_NAME_SIZE = 22; // Max size for names
const int MAX_SIZE = 25; // Max size for all other strings
// Employee Structure (Used for all employees
struct Employee {
// Chars used instead of strings for use of spaces
char eName[MAX_NAME_SIZE], eLName[MAX_NAME_SIZE], eStreetName[MAX_SIZE], eCity[MAX_SIZE], eState[MAX_SIZE];
int eID, eStreetNum, eZip;
void inNameeID();
};
// Max amount of employees allowed in program
const int MAX_EmployeeS = 50;
// The Employees class
class Employees {
public:
// Fuction Prototypes
void inEmployees();
void inNameeID();
void sortNameeID();
void print(const char *prompt);
// Private variables
private:
Employee data[MAX_EmployeeS];
int size;
};
// Main Processing
int main() {
//xEmployee.print("Original list"); // Used to print original
//xEmployee.print("Alphabetized list"); // Used to print aplha list
// Create new instance of Employees (stores all employee's data)
Employees xEmployee;
// Display which employee is being input
xEmployee.inEmployees();
// Prompt for input of the employee data
xEmployee.inNameeID();
// Sort the employee data by last name
xEmployee.sortNameeID();
// Print the employees that have been entered
xEmployee.print("Sorted Employee List");
//Pause program before shutting down
system("PAUSE");
}
// Prompt for the user input on the employee's data
void Employee::inNameeID() {
cout << "First Name: ";
cin.getline( eName, MAX_NAME_SIZE); // First Name
cout << "Last Name: ";
cin.getline( eLName, MAX_NAME_SIZE); // Last Name
cout << "Street Number: ";
cin >> eStreetNum; cin.ignore(); // Street Number
cout << "Street Name: ";
cin.getline( eStreetName, MAX_SIZE); // Street Name
cout << "City: ";
cin.getline( eCity, MAX_SIZE); // City
cout << "State: ";
cin.getline( eState, MAX_SIZE); // State
cout << "Zip Code: ";
cin >> eZip; cin.ignore(); // Zip Code
cout << "Customer ID: ";
cin >> eID; cin.ignore(); // Customer ID
cout << endl;
}
// Prompt for how many employees should be entered
void Employees::inEmployees() {
cout << "Enter number of Employees (1 . . " << MAX_EmployeeS << ")" << endl << '>';
cin >> size; cin.ignore();
}
// Display which employee is being entered
void Employees::inNameeID() {
for ( int count = 0; count < size; count++ ) {
cout << "Employee " << (count + 1) << endl;
data[count].inNameeID();
}
}
// Print the employees that have been entered
void Employees::print(const char *prompt) {
cout << prompt << endl;
cout << "------------------------------" << endl;
for ( int count = 0; count <size; count++ ) {
cout << data[count].eLName << ", " << data[count].eName << "\n"; // Last Name, First Name
cout << data[count].eStreetNum << " " << data[count].eStreetName << "\n"; // Street Number Street Name
cout << data[count].eCity << ", " << data[count].eState << " " << data[count].eZip << "\n"; // City, State Zip
//cout << setw( 8 ) << left << "Customer ID: " <<data[count].eID << "\n"; // Customer ID
cout << endl;
}
cout << endl;
}
// Sort fuction used to sort through the employee struct and sort by last name else, customer ID
void Employees::sortNameeID() {
int smallestOrLargest;
int index;
for ( int i = 0; i < size-1; i++ ) {
int smallestOrLargest = i;
for ( int index = i + 1; index < size; index++ ) {
int cmpName = strcmp(data[index].eLName, data[smallestOrLargest].eLName);
if (cmpName<0) { smallestOrLargest = index;
} else if (cmpName==0) { // names match
if (data[index].eID<data[smallestOrLargest].eID) {
smallestOrLargest = index;
}
}
}
if (smallestOrLargest != i) { //swap them
Employee temp = data[i];
data[i] = data[smallestOrLargest];
data[smallestOrLargest] = temp;
}
}
}
Obviously it isn't completed from the description, but I thought you guess/gals(if any) might want to see how it turned out!!