vector template with stl member function

Hello,

I am trying to add a Try-Catch and throw 'exception' for possible wrong input data. I've also got to add a vector template container and at least one member function of the vector template from STL and it has to be implemented into my original code which comes after the following paragraph. Can anyone help me implement these things:

1) A Try-Catch & Throw (exception handler) for wrong input data
2) Add a vector template container with at least 1 member function of the vector template from the STL.

#include <iostream>

using namespace std;

class Studentrecords
{
private:

struct student
{
int studentID;
string name;
string address;
double gpa;
};

student *stackArray;
int stackSize;
int top;

public:

Studentrecords();
Studentrecords(int size);
~Studentrecords();
void push(int studentid, string name, string address, double gpa);
void pop();
bool isFull() const;
bool isEmpty() const;
void display();
};

Studentrecords::Studentrecords(int size)
{
stackArray = new student[size];
top = 0;
}

Studentrecords::Studentrecords()
{
stackSize = 21;
stackArray = new student[stackSize];
top = 0;
}

Studentrecords::~Studentrecords()
{
delete [] stackArray;
}

void Studentrecords::push (int studentid, string name, string address, double gpa)
{

if (isFull())
{
cout << "The stack is full!" << endl;
}

else
{
student newStudent;
newStudent.studentID = studentid;
newStudent.name = name;
newStudent.address= address;
newStudent.gpa = gpa;
stackArray[top] = newStudent;
top++;
}
}

void Studentrecords::pop()
{
if (isEmpty())
{
cout << "The stack is empty!" << endl;
}

else
{
cout << stackArray[top-1].studentID << ", "
<< stackArray[top-1].name << ", "
<< stackArray[top-1].address << ", "
<< stackArray[top-1].gpa << endl;
top--;
}
}

bool Studentrecords::isFull() const
{
bool status;

if (top == stackSize - 1)
status = true;

else
status = false;

return status;
}

bool Studentrecords::isEmpty() const
{
bool status;

if (top == -1)
status = true;

else
status = false;

return status;
}

void Studentrecords::display()
{

for (int i = 0; i< top; i++)
{
cout << stackArray[i].studentID << ", "
<< stackArray[i].name << ", "
<< stackArray[i].address << ", "
<< stackArray[i].gpa << endl;
}
}

int main()
{
Studentrecords stack;

cout << "Pushing 20 records into the array and displaying them...\n" << endl;

stack.push(251, "Percy Jackson", "1 Meadow Blvd", 3.2);
stack.push(455, "Mike Shell", "1435 Oriole Ave", 2.1);
stack.push(247, "Ed Kunz", "1423 Cardinal Ave", 2.9);
stack.push(111, "Hardy Menz", "6 Hillary St", 2.1);
stack.push(191, "Betty Boop", "5 Boop Terr", 2.7);
stack.push(208, "Happy Gilmore", "16 Happy St", 3.3);
stack.push(202, "Ray Shay", "346 Sunflower Dr", 2.1);
stack.push(151, "Bill Ding", "556 Moore Ave", 3.2);
stack.push(109, "Sue Tuch", "25 Mackenzie Dr", 2.3);
stack.push(574, "Ida B Happy", "333 Bellvue Ave", 3.4);
stack.push(654, "Dan Sharp", "23 Mockingbird Ln", 3.0);
stack.push(610, "Manny Flow", "44 Jackson St.", 3.6);
stack.push(619, "Carl Lewis", "91 Ent Cir", 3.8);
stack.push(675, "Stu May", "88 Hunt Ave", 3.3);
stack.push(955, "Pat Flat", "95 Emerson Square", 3.2);
stack.push(913, "Bart Simpson", "5502 Homer Ave.", 3.4);
stack.push(999, "Walt Disney", "1403 Disney Ln", 3.5);
stack.push(815, "Rhett Ray", "1092 Rant Blvd.", 3.7);
stack.push(811, "Shay Toe", "331 Bay St", 3.1);
stack.push(813, "Harry Toe", "45 104th Street", 3.5);
stack.display();

cout << "\n \nThe following 5 students are being popped out of the array! \n" << endl;
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();

cout << "\n \nThe remaining students left in the array are: \n" << endl;
stack.display();
system("PAUSE");
return 0;
}


Any help would be great! Thank you very much for your help!
Are you required to do #1? If so, I feel very very sorry for you. It's poor design and shouldn't be done that way.

As for #2, there are a few things you have to consider.

Your "Studentrecords" class seems to be a container, however it has very tight coupling. You should remove the "student" struct from it (just keep it outside the class) and you should rename it so it is more generic (it will be come your vector class). I'd suggest a name that doesn't include the word "student".

The other thing is that you will need to define all the member functions inline for it to become a template. Like this:
1
2
3
4
5
6
7
struct MyClass
{
    void f()
    {
        //definition right here, inside the class
    }
};


Lastly, you'll need to convert it into a template. This involves using the template keyword correctly, and replacing all references to "student" with the name of the template parameter.
Topic archived. No new replies allowed.