0

I created an array for studentrecords and am supposed to pop it into my stack.. well everything works except for my stack.pops and stack.pushes in MAIN...I am so close to finishing this program I am wondering if anyone knows any solutions?

#include <iostream>
#include <list>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>

using namespace std;

class Studentrecords
{
private:
    struct student
    {
        string name;
        string address;
        int ID;
        double gpa;
    };

    student *stackArray;
    int stackSize;
    int top;

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

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

Studentrecords::Studentrecords()
{
    stackSize = 20;
    stackArray = new student[stackSize];
    top = -1;
}

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

void Studentrecords::push (string name, string address, int id, double gpa)
{
    if (isFull())
    {
        cout << "The stack is full!" << endl;
    }
    else
    {
        student newStudent;
        newStudent.name = name;
        newStudent.address= address;
        newStudent.ID = id;
        newStudent.gpa = gpa;
        stackArray[top] = newStudent;
        top++;
    }
}

void Studentrecords::pop ()
{
    if (isEmpty())
    {
        cout << "The stack is empty!" << endl;
    }
    else
    {
        cout << stackArray[top-1].name << endl;
        cout << stackArray[top-1].address << endl;
        cout << stackArray[top-1].ID << endl;
        cout << 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].name << endl;
        cout << stackArray[i].address << endl;
        cout << stackArray[i].ID << endl;
        cout << stackArray[i].gpa << endl << endl;
    }
}

int main()
{
    int catchVar;

    Studentrecords stack();

    cout << "Pushing 1st";
    stack.push("Jonny", "123 ave", 2343, 3.2);

    cout << "pushing 2nd";
    stack.push("Robby", "123 ave", 2343, 3.2);

    cout << "Popping ";
    stack.pop(catchVar);
    cout << catchVar << endl;

    cout << "Popping ";
    stack.pop(catchVar);
    cout << catchVar << endl;

    return 0;
}
4
  • Does this code even compile? You're calling pop(catchVar) but pop() takes no parameters. Commented Sep 20, 2012 at 23:09
  • @GregHewgill no, it couldn't possibly compile. Commented Sep 20, 2012 at 23:11
  • you might want to work on your indention and reduce the code to the relevant parts. You might also want to mention what exactly the problems are. It's not really fun to wade through less then desiarable indented code without knowing what one is looking for... Commented Sep 20, 2012 at 23:11
  • 1
    You kind of forgot to tell us what the problem is. Just telling us that something doesn't work isn't very helpful. Think about it like going to a mechanic -- would you just say "my car doesn't work"? Or would you tell him what precisely you were doing and what precisely went wrong? Commented Sep 20, 2012 at 23:20

2 Answers 2

6
Studentrecords stack();

Does not declare a Studentrecords named stack, it declares a function named stack that returns a Studentrecords. Change it to

Studentrecords stack;

Also your class needs at least a copy constructor and assignment operator.

Sign up to request clarification or add additional context in comments.

5 Comments

Incidentally, clang++ provides a very clear warning for this most vexing parse. It's a shame GCC doesn't.
Or Studentrecords stack{}; if you have a C++11 compiler
@Prætorian that's unnecessary though, so best to just leave them off.
In this case, it is. But what if Studentrecords was a POD?
@KevinBallard unfortunately most people learning C++ do so at a university on a very old compiler or using Windows at home, they don't have a chance to use clang.
0

Can you post the compiler's error? Or the produced output VS the expected output? Without that, I'll have to say that your pop function doesn't take arguments and you are passing it catchVar... that would be a compiler error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.