Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

//Client File

/*
Isaac Bernal
Fundamentals of Programming 2
Professor Lara
4/14/22
This program demonstrates the use of linear search on a list of faculty members' first name, last
name, and I.D.
*/

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include "Faculty.h"
using namespace std;

//Function Prototypes
void welcome();
int LinearSearch(Faculty[], int, string);
int getData(Faculty[], const int );
void getKey(string&);
void printData(Faculty[], int);

int main() {
const int SIZE = 50;
Faculty instructorArray[SIZE];//Class Array
ifstream inputfile;//to access files
string searchKey;//user's search
int lcount;//total members in array
int keyCount;//location of last name

welcome();

lcount = getData(instructorArray, SIZE);

do
{
getKey(searchKey);

keyCount = LinearSearch(instructorArray, lcount, searchKey);//counter/location that is


returned is stored in keyCount
if (keyCount == -1)//validation
{
cout << "\n**oopsie daisy, Try again!**\n";
}

}while(keyCount < 0);

printData(instructorArray, keyCount);

return 0;
}

//Function Definitions

//***********************************************************
void welcome()
{
cout << "\t\t_-_Welcome To Faculty Member Search_-_\n\n";
}

//***********************************************************
//To Receive the last name and search through array to find the matching last name
// Returns the counter in which the last name sits on the array
int LinearSearch(Faculty listArray[], int size, string key)
{
int counter;

for (counter = 0; counter < size; ++counter) //loop to access each the objects in array
{
if (listArray[counter].getLastName() == key)//When name is found matching the array variable
{
return counter;
}
}
return -1;//if not found
}

//***********************************************************
//Reads the text file and store in array
//returns the counter of total members of the array
int getData(Faculty arrayN[], const int SIZE)
{
istringstream inSS;//each members data is in a single line
ifstream inputfile;// to access txt file
inputfile.open("list.txt");
string lineString;

string fName , lName;//stores its own data from file


short iD;
int counter = 0;
while (counter < SIZE)
{
getline(inputfile, lineString);//stores each line of data in a string
inSS.clear();
inSS.str(lineString);//stores buffer in string
inSS >> fName >> lName >> iD;//assign to each variable

arrayN[counter].setFirstName(fName);//set to class to be stored


arrayN[counter].setLastName(lName);
inputfile.clear();

arrayN[counter].setID(iD);

counter = counter +1;


}

inputfile.close();

return counter;
}

//***********************************************************
void printData(Faculty fArray[], int keyCount)
{
cout << "\tName: " << fArray[keyCount].getLastName() << ", " <<
fArray[keyCount].getFirstName() << "\n\tFaculty I.D.: " << fArray[keyCount].getID();
}

//***********************************************************
//Asks user for the last name
void getKey(string& sKey)
{
cout << "Enter Last Name of Faculty Member: ";
cin >> sKey;
}
//* --- This file is the IMPLEMENTATION FILE ---

#include <string>
using namespace std;
#include "Faculty.h"

//Class Function Definitions


void Faculty::setFirstName(string f)
{
firstName = f;
}

void Faculty::setLastName(string l)
{
lastName = l;
}

void Faculty::setID(short i)
{
facultyID = i;
}

string Faculty::getFirstName()
{
return firstName;
}

string Faculty::getLastName()
{
return lastName;
}

short Faculty::getID()
{
return facultyID;
}
/*
--- This file is the SPECIFICATION FILE ---
*/

#include <string>
using namespace std;

#ifndef FACULTY_H
#define FACULTY_H

class Faculty
{
private:
string lastName;
string firstName;
short facultyID;
public:
//Prototypes
void setFirstName(string f);
void setLastName(string l);
void setID(short i);
string getFirstName();
string getLastName();
short getID();
};

#endif

You might also like