//CS210 ASSIGNMENT 3 SAMPLE SOLUTION
//
//THIS PROGRAM STORES STUDENT DATA AS AN ARRAY OF RECORDS.
//THE DETAILS INPUT FOR EACH STUDENT ARE:
// FIRSTNAME, LASTNAME, STUDENT NUMBER AND AVERAGE GRADE.
//THE PROGRAM WILL ALLOW THE USER TO:
// 1. ADD A NEW RECORD
// 2. PRINT RECORDS
// 3. FIND A RECORD BASED ON FIRSTNAME

 

#include <iostream>
#include <string>

// struct Student
// this struct holds student data
//

 struct Student
 {
    string FirstName;
    string LastName;
    int StudentNum;
    double GradePointAvg;
 };

 

// function prototypes
//ReadData() -reads the details for one student into the struct
void ReadData(Student &s);

//PrintRecord() -prints the data for one student
void PrintRecord(Student s);

//Initialise() -initialises the fields of a struct.
void Initialise(Student &s);
 

// constant to hold the maximum number entries in array SData.
const int MAX_ENTRIES = 128;

 

//function prototypes
//InitialiseData() -initialises each struct entry in the array
//SData[]. This function calls the function Initialise().
void InitialiseData(Student SData[]);

//FindData() -searches for a struct entry in the array which matches
//the the input string name.
void FindData(Student SData[], int numEntries, string name);

//PrintData() -prints the complete array of struct entries. This
//function calls the function PrintRecord().
void PrintData(Student SData[], int numEntries);

 
//AddRecord() -places a new struct/record in the next position in the
//array SData[]. This function calls the function ReadData().
void AddRecord(Student SData[], int &numEntries);

 

 
int main(){ // create an array of struct of size MAX_ENTRIES

// and initialise the array.
Student SData[MAX_ENTRIES];

string name; // string to hold the input string name
             // for the function FindData()
int numEntries = 0; // integer counter to hold the number
                    // of records in the array SData().

// Call the function InitialiseData() to initialise each array
// elemennt in the array SData() InitialiseData(SData);
// A menu is presented, and an operation is performed depending on the user
// input

char choice; // character variable to hold user choice.

do {
    cout << "(a)dd a record" << endl;
    cout << "(l)ist records" << endl;
    cout << "(f)ind a record" << endl;
    cout << "(q)uit" << endl;
    cout << "> " ;
    cin >> choice;

    // we use cin.ignore to remove the carriage-return from the
    // input stream after cin, in order for getline() to work
    // correctly afterwards.

    cin.ignore();
    if (choice == 'a')
        { AddRecord(SData, numEntries); }
    else if (choice == 'l')
        { PrintData(SData, numEntries); }
    else if (choice == 'f') {
                    // get the name to search for in the array.
        cout<<"Enter name"<<endl; getline(cin,name);
        FindData(SData,numEntries,name);
        }
    } while (choice != 'q');
}

 

 
// initialiseEntry//
// sets the contents of an entry record to be empty
//

void Initialise(Student &s)
{
    s.FirstName = "";
    s.LastName = "";
    s.StudentNum =0;
    s.GradePointAvg = 0.0;
}

// InitialiseData
// Initialises each struct entry in the array SData[] by calling
// the function Initialise().

void InitialiseData(Student SData[])
{
    for(int i=0;i<MAX_ENTRIES;i++)
    Initialise(SData[i]);
}
 

//
// ReadData 
// reads information from the user into a struct.
//

void ReadData(Student &s)
{
    cout << "Enter FirstName: ";
    getline(cin, s.FirstName);
    cout << "Enter LastName: ";
    getline(cin, s.LastName);
    cout << "Enter id: ";
    cin>>s.StudentNum;
    cout << "Enter points average: ";
    cin>>s.GradePointAvg;
}

//
// AddRecord
// A record is added to the array
// numEntries is passed by reference (using the & ) as this variable
// will be changed by the function AddRecord().

void AddRecord(Student SData[], int &numEntries)
{
    if (numEntries < MAX_ENTRIES) {
    ReadData(SData[numEntries]);
    numEntries++;
    }
}

 
// PrintRecord
// prints out the information in Student record.
//

 
void PrintRecord(Student s)
{
    cout << "\tname: \t\t" << s.FirstName + ' ' + s.LastName << endl;
    cout << "\tid: \t" << s.StudentNum << endl;
    cout << "\tgradeavg: \t\t" << s.GradePointAvg << endl;
}

// PrintData
// Prints numEntries number of records. This function calls the
// function PrintRecord(). The parameter numEntries in the function
// PrintData() is passed by value as it is not changed by the
// function PrintData().

void PrintData(Student SData[], int numEntries)
{
    for(int i=0;i<numEntries;i++)
    PrintRecord(SData[i]);
}

 

//
// FindData
// Searches for a record which matches the string name
// and outputs the string found
//

 

void FindData(Student SData[], int numEntries, string name)
{
    bool found = false; // initialise found to false
    int i=0;
 
    while( (!found) && (i < numEntries)) {
           // while the name is not found and there  are still
           // records in the array
        if(name == SData[i].FirstName){
                found = true;
                cout<<endl<<SData[i].FirstName<<endl;
            } // end if
        i++; // increment i
        } // end while
 
    if(!found)
        cout<<endl<<name<<" not found"<<endl;
}