returning error on if/else in function

This is a discussion on returning error on if/else in function within the C++ Programming forums, part of the General Programming Boards category; i overloaded the operator '-' to subtract 2 Distances. i dont want to accept negative distance so how do i ...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63

    returning error on if/else in function

    i overloaded the operator '-' to subtract 2 Distances. i dont want to accept negative distance so how do i exit the function? i tried "return -1;" but the return type is of type Distance. do i have to do the error checking in main?
    Code:
    Distance Distance::operator - (Distance d2) const	//return difference
    {
    	int f = feet - d2.feet;	//subtract feet
    	float i = inches - d2.inches;	//subtract inches
    
    	if(i < 0)
    	{
    		--f;
    		i = 12 - (i * -1);
    	}
    
    	if(f < 0)
    	{
    		cout << "Error:	Negative Distance." << endl;        //this line doesnt print for some reason
    		return Distance(0,0.0);
    	}
    	else
    		return Distance(f,i);
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You could always throw an exception.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    the book im following hasnt discussed exceptions yet. is there, perhaps, another way? thanks for the reply though

  4. #4
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Maybe the operator could return the magnitude of the difference between the distances. That seems more logical.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you don't want to use exceptions, then the way you're doing it looks fine.

    cout << "Error: Negative Distance." << endl; //this line doesnt print for some reason

    Are you sure f is coming up negative? Maybe do a:
    cout << "f:" << f << endl;
    to be sure.

  6. #6
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    f is coming up negative, found a way using exit() in stdlib.h thanks guys

  7. #7
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    found a way using exit() in stdlib.h
    You mean you're exiting the whole program just because you got a negative distance? Seems a bit extreme to me.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. function returning hour in either 12 or 24 hour format
    By stanlvw in forum C Programming
    Replies: 4
    Last Post: 01-01-2008, 05:02 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 10:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21