473,416 Members | 1,547 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Function pop causing crash

The program below fails on execution and I think the error is in my
pop function but it all looks correct.Also could someone check my code
as to why my print function is not
working? I havent included the other parts of my program but will if
someone needs it. Please help; I have had it. I have checked all C++
websites and cannot figure it out.
<code>

template<class Type>
void Novice<Type>::Print()
{
while(! IsEmpty())
{
std::cout << topPtr->item;
topPtr = topPtr ->next;
}
}
template<class Type>
void Novice<Type>::Pop(Type &y)
{
if(IsEmpty())
throw EmptyStack();
else
{
Node<Type*tempPtr;
tempPtr = topPtr;
topPtr = topPtr -next;
delete tempPtr;
}
}
// main function

int main()
{
Novice<inta;
int s;

a.Pop(s); // not working
a.Push(1);
a.Push(2);
a.Push(3);
cout << a.length() << endl;
a.Pop(s);
cout << a.length() << endl;
a.Print();
}

</code>

Apr 15 '07 #1
4 2539
I guess you are popping from an empty stack and it is throwing an
exception because IsEmpty will be true. Wrap the code in a
try{}catch() block and catch the exception. Hope that helps.

Apr 15 '07 #2
j_*******@yahoo.com wrote:
The program below fails on execution and I think the error is in my
pop function but it all looks correct.Also could someone check my code
as to why my print function is not
working? I havent included the other parts of my program but will if
someone needs it. Please help; I have had it. I have checked all C++
websites and cannot figure it out.
<code>
If you want some decent help, post something that compiles, otherwise we
have to guess what a Novice and a Node are.

--
Ian Collins.
Apr 16 '07 #3
Prashanth wrote:
I guess you are popping from an empty stack and it is throwing an
exception because IsEmpty will be true. Wrap the code in a
try{}catch() block and catch the exception. Hope that helps.
Please keep the context you are replying to. This post makes no sense
on its own.

--
Ian Collins.
Apr 16 '07 #4
On Apr 15, 4:54 pm, j_depp...@yahoo.com wrote:
The program below fails on execution and I think the error is in my
pop function but it all looks correct.
And thats exactly what the program should do - fail. You are
attempting to pop an empty container and you aren't handling the
expected and thrown exception. Thats assuming that IsEmpty() is
working properly.
Since relying on educated guesses and assumptions is what we are asked
to disscuss here: try compiling and running the following program for
illustration.

#include <iostream>
#include <stdexcept>

class EmptyStack : public std::runtime_error
{
public:
EmptyStack()
: std::runtime_error("EmptyStack error") { }
};

int main()
{
try {
throw EmptyStack();
}
catch(const std::exception& r_e)
{
std::cout << "error: " << r_e.what();
std::cout << std::endl;
}
}

/*
error: EmptyStack error
*/

The std::runtime_error type is derived from std::exception. The thrown
exception is therefore caught by the above catch block. What should
the program do if i were to throw EmptyStack() and don't bother
catching it? Try it.

int main()
{
throw EmptyStack();
}

Does the result look familiar to you?
Note: you don't have to derive from anything insofar as the type
EmptyStack is concerned. You can throw anything, as long as you use a
try block and catch it.

class EmptyStack { };

int main()
{
try {
throw EmptyStack();
}
catch(const EmptyStack& r_e)
{
std::cout << "error: EmptyStack" << std::endl;
}
// other catch blocks...
}
Also could someone check my code as to why my print function is not
working? I havent included the other parts of my program but will if
someone needs it. Please help; I have had it. I have checked all C++
websites and cannot figure it out.
<code>

template<class Type>
void Novice<Type>::Print()
{
while(! IsEmpty())
{
std::cout << topPtr->item;
topPtr = topPtr ->next;
}

}
The function is not 'emptying' the container in any way (IsEmpty() is
never satisfied). You are therefore stepping through ghost pointers
(topPtr->next). Anything can happen here - the result is undefined.
>
template<class Type>
void Novice<Type>::Pop(Type &y)
{
if(IsEmpty())
throw EmptyStack();
else
{
Node<Type*tempPtr;
tempPtr = topPtr;
topPtr = topPtr -next;
delete tempPtr;
}

}

// main function

int main()
{
Novice<inta;
int s;

a.Pop(s); // not working
a.Push(1);
a.Push(2);
a.Push(3);
cout << a.length() << endl;
a.Pop(s);
cout << a.length() << endl;
a.Print();

}

</code>

Apr 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jim C Nguyen | last post by:
I have a table with ~2.2 million rows. Sometimes when I do an update to one single row it will instead update ALL of the rows using the same update. This happens every one in about 500,000...
1
by: bugman | last post by:
The problem started with Firefox. Got to www.foxnews.com and click on any of the scrolling headlines in the right hand box. Firefox would crash. Now Opera and IE do also. If I disable...
2
by: shree | last post by:
Hi all, I'm trying to call javascript function via onLoad but running into a problem. I would appreciate any help and suggestion. My application is as follows. When a page loads, I want to check...
7
by: Jamma | last post by:
Hello, I have an Access 2000 file working on XP machines and a NT network. The files are split and 95% of the time all is well and stable. The main issue arises when the app prints,a novell...
3
by: Brad | last post by:
I have an aspx web page which initially displays fine. When I postback the resulting response back to the client output is causing InternetExplorer 6 to crash. I've disabled the server side code...
9
by: Roshni | last post by:
Hi, I wanted to know how do function pointers sometime access illegal memory access ? Could any one give me an example ? Thanks, Roshni
0
by: J.P. | last post by:
Hi, I'm having an issue after IE7 is installed on a work station, programs set to run in full trust over the intranet are crashing. I've tried adding the machines where the programs reside to...
25
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
5
by: q3537wh | last post by:
Has anyone encountered a situation where the @import is causing IE 6 to crash? I have a css file, let's call it test.css, that has only two lines in it. @import url("file1.css"); @import...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.