473,545 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Ending Function on Error then Continue in main()

I have a situtation where if a overloaded operator is used incorrectly I
need it to cout some info to the screen, end the function it was called in,
and continue on in main. How on earth do you do that?

The exact example can be found at http://planetevans.com/c under test 17,
18, and 19.

One of the specific examples is

test17()
{
cout << "17. Array declared with illegal array bounds: IntArray f(5,
2);" << endl << endl;
IntArray f(5, 2); //illegal becuase it is trying to define an
array with an index going from 5 to 2. The constructor is show below
for(int i = f.low(); i <= f.high(); ++i) //dont want this to run
f[i] = i * 10; //dont want this to run
cout << f << endl; //dont want this to run
}

IntArray::IntAr ray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayHigh>=a rrayLow)
array=new int[arrayHigh-arrayLow+1]; //create array memory
locations
else
cout << "Low Array Boundry Higher than High Array Boundry" << endl;
//now I need to end the function that this was called from
}
Jul 19 '05 #1
3 2915

"Heinz Ozwirk" <wa******@gmx.d e> wrote in message
news:bg******** *****@news.t-online.com...
"- Steve -" <se****@foundat ion.sdsu.edu> schrieb im Newsbeitrag
news:t0******** ***********@new s2.central.cox. net...
: I have a situtation where if a overloaded operator is used incorrectly I
: need it to cout some info to the screen, end the function it was called
in,
: and continue on in main. How on earth do you do that?
Avoid reporting errors on the screen in functions you (or others) might use again in other programs. Report them to the calling program and let the
program (and >>its programmer) decide how to handle them. That's what
exceptions have been invented for. Have a look at try/catch/throw and
std::exception.
Regards
Heinz


Unfortantley it's a requirment for the assignment I've been given.


Jul 19 '05 #2


- Steve - wrote:

test17()
{
cout << "17. Array declared with illegal array bounds: IntArray f(5,
2);" << endl << endl;
IntArray f(5, 2); //illegal becuase it is trying to define an
array with an index going from 5 to 2. The constructor is show below
for(int i = f.low(); i <= f.high(); ++i) //dont want this to run
f[i] = i * 10; //dont want this to run
cout << f << endl; //dont want this to run
}

IntArray::IntAr ray(int low, int high)
{
arrayLow=low; //start of array index
arrayHigh=high; //end of array index
if(arrayHigh>=a rrayLow)
array=new int[arrayHigh-arrayLow+1]; //create array memory
locations
else
cout << "Low Array Boundry Higher than High Array Boundry" << endl;
//now I need to end the function that this was called from
}

As Heinz has already told you: you could throw an exception in the constructor.
Another possibility would be:

class IntArray
{
public:
IntArray( int LowBound, int HighBound );

bool IsGood();

...
};

void test17()
{
IntArray f( 5, 2 );

if( !f.IsGood() )
return;

...
}
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #3
"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:<c%******* ************@ne ws2.central.cox .net>...
"Heinz Ozwirk" <wa******@gmx.d e> wrote in message
news:bg******** *****@news.t-online.com... "- Steve -"
<se****@foundat ion.sdsu.edu> schrieb im Newsbeitrag
news:t0******** ***********@new s2.central.cox. net...
: I have a situtation where if a overloaded operator is used
: incorrectly I need it to cout some info to the screen, end the
: function it was called in, and continue on in main. How on earth do
: you do that?
Avoid reporting errors on the screen in functions you (or others)
might use again in other programs. Report them to the calling
program and let the program (and its programmer) decide how to
handle them. That's what exceptions have been invented for. Have a
look at try/catch/throw and std::exception.Regards Heinz


Unfortantley it's a requirment for the assignment I've been given.


The "Avoid reporting errors on the screen..." is a recommendation that
you should avoid it in general. If you need to do it here, then do it.

The advice on using exceptions is orthogonal, and is what you need.
Note that it isn't an overloaded operator you're using incorrectly, but
the constructor. Anyway, your code should probably look like:

#include <stdexcept>

class IntArray
{
public:
IntArray(int low, int high)
{
if( low > high )
// add output here if you need it
throw std::range_erro r("IntArray: low > high");

//...your code...
}
};

void test17()
{
IntArray f(5,2); //this will throw straight away

//...this code will not be reached...
}

int main()
{
try {
//...
test17();
//...

} catch( std::exception& ex ) {
// we come straight here after IntArray::IntAr ray
// throws std::runtime_er ror, skipping the rest of
// test17().
}
}
Jul 19 '05 #4

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

Similar topics

6
1884
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of this function. bool processdrecord( ofstream& prnfile, ofstream& validdata, char* record ) { drecord Newdrecord; char customercode;
38
2499
by: Red Dragon | last post by:
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery. Why does the computer refuse to execute my scanf ("%c",&q); On input 3 4 1 (for a,b and c) I had real roots OK On input 1 8 16 I had same real roots OK. However on 4 2 ...
27
2055
by: cj | last post by:
I run this program and to exit click the X in the upper right corner. But apparently it isn't really ending the program. If I return to VB and make changes then try to rebuild the app it says the exe is still in use--I find it is still a process in Task Manager. What do I need to do to make clicking that X actually end the program? ...
10
10022
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to believe that this has to do with unreleased form component events or event handlers. I'm comparatively new to .net and windows forms, in the sense that...
4
2178
by: wesbland | last post by:
>From my understanding, when a string is stored in VB.NET and you look at it in the debugger, it has a quote on both sides to signify that it is a string as opposed to a char or int or whatever. I've got a simple program here (I actually found it on the web somewhere, but I'm looking through it) that doesn't seem to have that ending quote at...
1
1746
by: Randeh | last post by:
Newb C++ problem, arithmetic functions are returning 0 values each time. Not sure where the problem is, but I've run checks and it seems to be going fine until the very end where the arithmetic functions are: #include <iostream> #include <iomanip> #include <cstring> using namespace std; const int SIZE = 41; char TestOp, UseOp, TestNum;
34
2702
by: Umesh | last post by:
I want to extract a string abc*xyz from a text file. * indicates arbitrary no. of characters. I'm only able to do it when the string has definite no. of characters or the string length is constant: i.e. five or the string is abc????? xyz How can i generalize it for any length of the string?
2
1379
by: Alenik1989 | last post by:
I wrote this code, but I'm not able to test it because im getting 3 errors. So I'm not sure that this code will work properly or not!!! the code in #include <stdio.h> #include <stdlib.h> int rnd(char s1,char s2,char c); int GetSt(char s1,char s2,char c); void SearchAndPrint (char s1, char s2, char c, int length);
4
3281
by: istillshine | last post by:
I have a function foo, shown below. Is it a good idea to test each argument against my assumption? I think it is safer. However, I notice that people usually don't test the validity of arguments. For example, #define MAX_SIZE 100000 int foo(double *x, unsigned ling sz, int option) {
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7423
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5329
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3443
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.