Flagging errors by returning an error code with given global const int variales

I am completely lost with in terms of how a global variable can return an error code with a global const int. So I wrote one function that basically acquires the appropriate number of numbers for a given formula. I am able to invoke it in my int main function and it runs whenever I run it in cloud9. The problem arises when I submit it because it expects me to flag the error for the function with the given variable.
The instructions are:
returns the integer representing the state of the calculation using global constants for DIVISOR and DIVIDE_ZERO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  // the global variable that I am supposed to be using for this function
const int DIVISOR = 0;
const int DIVIDE_ZERO = 1;
// my code for the division function

int division(double a, double b, double &result){
    double answer;
    answer = (a/b);
    cout << "Equation: " << a << "/" << b << endl;
    if ((b - 0) <= .000001 ){
        cout << "Error: cannot divide by zero." << endl;
    }
    else{
        cout << setprecision(6);
        cout << fixed;
        cout << "Result: " << answer << endl;
    }
    
    return answer;
}


I really just need help with how to return the error with the global variable.
Last edited on
With integers, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const int NO_ERROR = 0 ;
const int DIVIDE_BY_ZERO = 1 ;
const int NOT_A_NUMBER = 2 ;

int division( int a, int b, int& result )
{
    if( b != 0 ) 
    {
       result = a / b ;
       return NO_ERROR ;
    }
    
    else if( a == 0 ) return NOT_A_NUMBER ; // 0/0
    
    else return DIVIDE_BY_ZERO ; // a/0
}


With floating point, there are other tricky issues; probably things that a beginner should not be required to address.
Topic archived. No new replies allowed.