0

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;
}
  • I don't see a question here. Other than 'help me with homework'. Please ask a specific question if you need help. – cigien Apr 05 '20 at 21:52

1 Answers1

0

Your program so far crashes because of a segfault, which is an error that occurs when you attempt to read from/write to memory that you are not allowed to access. An easy way to do this is to access invalid indices of an array (e.g. if I have int myArr[50]; and try myArr[250] = 10; I would get a segfault and the program will crash). In your program, I believe this same thing occurs because of the condition of your while loop (!inputfile.eof()). As I understand it, !inputfile.eof() only becomes true after you have attempted to read past the end of your file, so only after you finish reading the last line of your file (up to line 50 in this case) and attempt to read line 51 will this become true, but by the time this happens you are already inside of the loop body and must finish, where your iterator classSize is already 50 -- and so you are attempting to access instructorArray[50] (recall that your first 50 indices are from 0 to 49). This is past the length of the array, which is invalid, and it crashes. Instead, you should change your while condition to while(inputfile >> first >> last), which semantically translates to "While I can read in a value for both first and last".

If you would like more information about why iostream::eof() is considered bad as a looping condition, check out this Stack Overflow question where much smarter people go into more detail.

Happy coding!

Shaavin
  • 116
  • 6