I am new to programming so please, so I know I am just making a mess. My assignment is as follows....
Write a program that takes the name of instructors from a text file. It then places the names into an array of objects. Allow the user to search for an instructor (using the last name) using the Linear Search algorithm. If found, the program should return the name of the instructor.
Name the file as: facultySearch.cpp (EXACTLY like this. Copy and paste it if you need to)
Follow this format:
The class name is: Faculty It should have private members: firstName (string), lastName (string), and facultyID (short) It should have public members: setters, getters, and a constructor (to setup default information)
The function ‘linearSearch’ does not have to be part of the class.
Create an external text file with the instructor information.
In main: Create an array of Faculty object: instructorArray[] The array should hold up to 50 objects. Output the data as: lastName, firstName, facultyID (Make it a nice format)
Here is the code I have so far:
//classtxt60.cpp
//Put all header and description stuff here.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Faculty
{
private: //attributes go here (variables)
string firstName;
string lastName;
//short facultyID;
public: //methods go here (functions)
Faculty(); //constructor
void setfirstName(string first); //mutator
void setlastName(string last);
//void setfacultyID(short fID);
string getfirstName(); //accessor
string getlastName();
//string getID();
};
//Define the public functions here
Faculty::Faculty()
{
//The constructor will give defaults to
//the private members of my class
firstName = "No First Name";
lastName = "No Last Name";
}
void Faculty::setfirstName(string first)
{
firstName = first;
}
void Faculty::setlastName(string last)
{
lastName = last;
}
string Faculty::getfirstName()
{
return firstName;
}
string Faculty::getlastName()
{
return lastName;
}
const short SIZE = 50; //maximum class size
int main()
{
//Create an object of type Faculty
//All 25 objects are set to the default "No Name"
Faculty instructorArray[SIZE];
ifstream inputfile("source.txt"); //creates object to read from textfile & opens/links it to textfile
//Read from your file now.
//Use a while loop when you do not know how many items are in the text file.
string first; //holds name in file
string last;
short classSize = 0; //used as counter
while (!inputfile.eof())
{
//reads the name from the textfile
getline(inputfile, first);
getline(inputfile, last);
//if adding age or gender you would need to add a separate line for each
//places the name into the array
instructorArray[classSize].setfirstName(first);
instructorArray[classSize].setlastName(last);
//if adding age or gender you would need to add a separate line for each
classSize++; //moves to the next item
}
cout << first << " " << last << endl;
inputfile.close();
return 0;
}