problem with return

Hi
my fuction is working fine except when the user input 0 as divisor I just want to cout "Error!" and not return anything...
right now I cout "Error!" but the function also return 0 since I had to use a return...if I do not use return 0 then the function tries to perform the division by zero...and I get INF...
is there a way to tell return not to return anything at all?
Last edited on
If you have a return type, you must return a value.
Why don't you output the result in the function?

eg:
1
2
3
4
5
6
7
8
9
10
11
double displayDivision()
{
   //...
   if ( y!=0 )
   {
      cout << x/y;
      return x/y;
   }
   cout << "Error";
   return 0;
}
Another option would be to use exception handling. This is a case where no possible return value can tell the caller that there was a divide by zero. Zero is actually a possible return value so the caller would have no idea that there was an error.
Zero is actually a possible return value

only number/infinity = zero
There is nothing that precludes the numerator from being zero.

Edit:

You could say that x!=0 is a precondition, and then use zero as the return value which indicates a div by zero error.
Last edited on
try using the try... catch technique that is exception handling.
modify your main function to look like this:

int main()
{
double div;

try
{
div = displayDivision();
cout << div << endl;
}

catch (ComputCError e)
{
cout << "Error\n";
}

return 0;
}

then create an empty class

class ComputeCError{};

Now in your displayDivision() write this in the else portion:

else
throw ComputeCError();

throw statement basically throws an object of ComputeCError class which is caught by the catch and the statements in the catch block are executed.

This should work and solve the problem of the return...

Hope this helps




Please don't "help" by introducing more advanced concepts.

The problem is the way you are using the function.
cout << displayDivision() << endl;
This code says you are writing something to the standard output, no matter what or how that something is obtained. The command is not optional, and the kind of thing you are writing is always going to be the same thing (in this function's case, a double, not a string).

cout << "Enter first number: ";
The next problem is you are mixing an input operation into an output operation. This is a bad idea. Bazzy's suggestion methinks is best:
Why don't you output the result in the function?


Part of the problem is the name of the function, "displayDivision()". It is misnamed because it:
1 - doesn't display the division (rather, it returns the result of the division)
2 - it does more than display something -- it asks for input (the numbers to divide)

A better name might be something as silly as void getAndDisplayUserDivision(). This function gets two numbers from the user, divides them, and displays the result.

On a side note, you always break the loop so there is no need to have it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <everything>

void getAndDisplayUserDivision()
  {
  double x, y;

  cout << "Enter first number: ";
  cin >> x;
  cout << "Enter second number: ";
  cin >> y;

  if (y!=0)
    cout << x << " divided by " << y << " is " << x/y << endl;

  else
    cout << "Error!";
  }

int main()
  {
  char answer;

  do
    {
    getAndDisplayUserDivision();

    cout << "Do you want to continue (y/n)" << endl;
    cin >> answer;
    }
  while (answer!='n');

  return 0;
  }

As always, take care to use proper indentation and spacing to make your programs easier to read.

Hope this helps.
i was simply giving a possible solution not confuse him and i dont think that you can go much further writing larger programs without exception handling , you seem to be experienced and so should know that
Yes, but you don't teach 4th graders calculus.
It might come as a shock but large-scale systems don't have to use exceptions. I've worked on a number of projects that had exceptions turned off while compiling in gcc.
Dare I even say most don't use exceptions, even though they would probably benefit. Many engineers are of the mindset that exceptions are expensive, but it really depends on how the compiler implements them. At least older Msoft compilers implemented them in the most expensive way imaginable. GCC's compiled executables incur no penalties at runtime for using them.
I was going to say pretty much the same thing. The vast majority of large systems don't use exceptions -- they validate transactions. Exception handling is a relatively new thing...
why are exceptions expensive?
They aren't (or at least should not be) if you are using a reasonable compiler. I don't know if or when msoft fixed their exception support, but gcc has never been expensive.
Topic archived. No new replies allowed.