0

Hello everyone,

I've used this website many times to help myself based off of others' questions, but now I have a problem of my own that I can not solve. I was assigned the task of creating a stack program in C++ using a linked list. Seems simple enough, but I keep getting a segmentation fault error.

The program runs fine with variables that I define and push myself, but when I use input from a file the pop function no longer works correctly and I receive a segmentation fault.

The function at the bottom of the main file is to convert infix notated equations to postfix

eg: 1 + 3 would be 1 3 +

Here is my main file:

```
#include "stack.cpp"
int main(){
stack<int> intmain;
stack<char> charmain;
intmain.push(1);
intmain.push(2);
intmain.push(3);
std::cout << "intmain pop = " << intmain.pop() << std::endl
<< std::endl;
std::cout << "intmain pop = " << intmain.pop() << std::endl
<< std::endl;
std::cout << "intmain pop = " << intmain.pop() << std::endl
<< std::endl;
char a = 'a', b = 'b', c = 'c';
charmain.push(a);
charmain.push(b);
charmain.push(c);
std::cout << "charmain pop = " << charmain.pop() << std::endl
<< std::endl;
std::cout << "charmain pop = " << charmain.pop() << std::endl
<< std::endl;
std::cout << "charmain pop = " << charmain.pop() << std::endl
<< std::endl;
stack<char> infixtest, tempstorage;
char right = 0, left = 0, oper = 0;
char temp = 0;
std::ifstream in("data3-1.txt");
while (!in.eof()){
in >> temp;
if(temp == ')'){
right = tempstorage.pop();
std::cout << "popped :" << right << "to right." << std::endl;
oper = tempstorage.pop();
std::cout << "popped :" << right << "to oper." << std::endl;
left = tempstorage.pop();
std::cout << "popped :" << right << "to left." << std::endl;
infixtest.push(left);
infixtest.push(right);
infixtest.push(oper);
}
else{
if(temp != '(' && temp != ' '){
tempstorage.push(temp);
temp = 0;
}
}
}
return 0;
}
```

and here is the source file:

```
#include "stack.h"
template <typename T>
stack<T>::~stack(){
while(tos != 0){
node<T> *temp = tos;
tos = tos -> next;
delete temp;
}
}
template <typename T>
stack<T>::stack(stack<T>& actual){
node<T> *atemp, *temp;
tos = 0;
atemp = actual.tos;
while ( atemp != 0 ){
if ( tos == 0 ){
tos = new node<T>(atemp -> data);
temp = tos;
}else{
temp -> next = new node<T>(atemp->data);
temp = temp->next;
}
atemp = atemp -> next;
}
}
template <typename T>
void stack<T>::swap(stack<T>& B){
node<T> *temp = tos;
tos = B.tos;
B.tos = temp;
}
template <typename T>
stack<T>& stack<T>::operator = (stack<T> rhs){
swap(rhs);
return *this;
}
template <typename T>
bool stack<T>::isFull() const{
try{
node<T> *temp = new node<T>();
delete temp;
return false;
}
catch (...) { return true; }
}
template <typename T>
void stack<T>::push (T item){
try{
std::cout << "pushing " << item << std::endl;
if (isFull()) std::cout << "push error";
node<T>* temp = new node<T>();
temp -> data = item;
temp -> next = tos;
tos = temp;
}
catch (...){
std::cout << "Stack overflowed trying to push: "
<< item << std::endl << std::endl;
};
};
template <typename T>
T stack<T>::pop () {
node<T>* temp;
try{
if (isEmpty()) std::cout << "Stack is empty." << std::endl;
temp = tos;
if (tos -> data == 0){
return 0;
}
else{
tos = tos -> next;
return temp -> data;
}
}
catch ( char * str ){
std::cout << "Exception: Stack underflow -- "
<< str << std::endl << std::endl;
};
return -1;
};
```

any ideas/suggestions would be greatly appreciated!

*Edited 6 Years Ago by bkempert*: n/a