pobrien11 0 Newbie Poster

Hi,
I am completely lost on a program assignment. We are to use the stl stack adapter class. The elements of the stack are linked lists which represent polynomials. So in essense it is a stack of linked lists/polynomials. Note I'm NOT trying to make the stack itself a linked list, rather I'm trying to make the stack hold linked lists. I can get the code to work in int main(){} if I do:

#include <iostream>
#include "poly.h"
#include <stack>

using namespace std;

int main()
{

stack<poly*> st; // the stack of polynomial pointers

poly p1, p3; // create polynomial objects from the poly class

p1.getPoly(); // get the terms and create the 1st polynomial
cout << endl; // go to next line
cout << "The polynomial is: " << p1 << "\n"; 
p3 = p1.derivative(); // take the derivative  
cout << "The derivative is: " << p3 << endl;

poly* p2;
p2 = &p1;
st.push(p2);  // push the polynomial onto the stack
p2 = &p3;
st.push(p2);
p2 = st.top();
cout << *p2;
st.pop();
p2 = st.top();
cout << *p2;
st.pop();
cin.get(); // pause screen so can read the output
}

The code does not work for some reason when I declare poly* p2before calling the derivative function. I don't know why.

My problem lies in that I need to read a command sequence in from the screen and perform the commands. Simple Example: rdw, means ask for a polynomial, push onto the stack, take the derivative, pop the polynomial read in and push the derivative on, and then finally pop the derivative off the stack and write it to the screen. I tried making a function that processes the commands, i.e if it is 'r' then call a read function to create the polynomial and push it onto the stack, if it is 'w' call a write function that pops the stack and writes the polynomial. But garbage comes out when I call the write function. I attached all the files I'm using. Only the read and write functions are implemented so far for the interface. If anyone can help or tell me of someone who can, I would very much appreciate it. Thanks for reading,

Patrick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.