hello there. there's an error in my program that I cannot identify. Every time i call a function for my declared variable of type Stack, there will suddenly be a run time error. I really don't get what's wrong with my code because my implementation seems to be right but still I get this error. I hope someone could help me. Here are parts of my code:

class declaration:

Code:
class Stack{
private:
	node *head, *top;
public:
	Stack();
	void push(char[]);
	bool pop();
	char* peek();
	bool isEmpty();
	void display();
};


Stack::Stack(){
	head=top=NULL;
}

bool Stack::isEmpty(){
	if(top==NULL)
		return true;
	return false;
}

void Stack::push(char x[]){
	node *n = new node(x);
/*	if(n==NULL)
		return false;*/
	if(isEmpty())
		top = n;
	else{
		top->next = n;
		n->prev = top;
		top = n;
	}
//	return true;
}

bool Stack::pop(){
	if(isEmpty())
		return false;
	node *del = top;
	top = top->prev;
	if(top!=NULL)
		top->next = NULL;
	del->prev = NULL;
	return true;
}

char* Stack::peek(){
	return top->item;
}

I get an error whenever I perform something like this:
Code:
void main(){

Stack s;
	s.push(word);	//run-time error
	s.peek();	//run-time error	
}

please take a look at my class declaration/implementation, the error might be found there. I'm really bad at looking for errors, I swear.

Recommended Answers

All 9 Replies

Where do you define the type "node". I don't see where it is defined...

That and (if I'm reading your main() correctly) I don't see where "word" is coming from. All I see is an undeclared variable.

oh sorry. i forgot to include it here. I declared a class node. This is how it looks:
class declaration:

class node{
public:
	char item[15];
	node *next, *prev;
	node(char[]);
	node();
};

constructors:

node::node(char x[]){
	strcpy(item,x);
	next=NULL;
}

node::node(){
	strcpy(item,"0");
	next=NULL;
}

I have actually declared "word" as char in my actual program.. I just forgot to include it here...
I'm still stuck with the same problem.. And this assignment of mine is due on monday.. If I won't be able to pass it correctly, I might fail the subject :(

I have actually declared "word" as char in my actual program.. I just forgot to include it here...

So are you saying that you have declared ..

char word;

i.e. NOT ..

char word[some size here];

?

PS. You still 'forgot' to include it.

I declared it as

char word[15];

and it works well. :)
my problem is that run time error. i can't get rid of it :(

I declared it as

char word[15];

my problem is that run time error. i can't get rid of it :(

Perhaps you could tell us what this run time error actually is?
Moreover, have you been getting several different types of run time errors, and if so, which ones?

Maybe also tell which compiler you are using.

Yay I was already able to solve this problem but I've encountered yet another one... Let me post a part of my source code here:

void main(){
	char word[15], st1[15], st2[15];
	Stack s, tmp;
	Queue q;
	cout<<"Enter starting word: ";
	cin>>st1;
	cout<<"Enter ending word: ";
	cin>>st2;
	if(strlen(st1)!=strlen(st2))
		cout<<"Cannot form word ladder."<<endl;
	else{
		ifstream file ("dictionary.txt");
		s.push(st1);
		while (!file.eof()){
			if(s.peek()==NULL)
				s.push(st1);			//pushes starting word (st1) to stack s, thus st1 will always be found in every succeeding stack s
			file.getline(word,'\n');
			if((diff(st1,word)==true)){
				s.push(word);			//pushes word to Stack s
				tmp=s;						//stores s to tmp
		/*the while loop below is my problem, whenever Stack s becomes empty, the program terminates*/
				while(!s.isEmpty()){		//empties s for next storage, reiterates until s is empty
					cout<<s.peek()<<" = peek s"<<endl;
					s.pop();
				}

Actually, we were asked to make a word ladder connecting word 1 to word 2.

Yay I was already able to solve this

Nice.

>> whenever Stack s becomes empty, the program terminates
You mean 'terminates' as in; the program crashes? If so, what is the error message?

Note that using eof() like you do, generally is not a good idea. Dave Sinkula tells you why here.

Error message: caoile_3.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

Yes, the program crashes :(

thanks for that link. But may I ask, what's that int i that he declared? what's it for?

Yes, the program crashes

By looking at the initial code you had and the code that crashes, I'm guessing that something like the following happens ..

struct test
{
  const char * peek(bool return_pointer_to_char)
  {
    if(return_pointer_to_char == true) {
      // return a valid char *
      return "testing";
    }

    // No return value specified in this case,
    // program's behaviour is pretty unpredictable
  }
};

int main()
{
  test t;

  // This cout will work
  cout << t.peek(true) << endl;

  // But this one will not, you are likely to
  // see a crash of some sort
  cout << t.peek(false) << endl;

  return 0;
}

So, you have to make sure that anytime a function that is declared to return something, it really returns a valid value.

>> But may I ask, what's that int i that he declared? what's it for?
The int i was used for receiving the integer values read from the file. You might try out those programs with the input files demonstrated, to see how they actually work and moreover, to see how the usage of eof() is bad.

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.