Function to determine if program should continue.

User inputs a value for x and a value for y, then I have this for loop output a series of dots in a vertical line which I then import into excel and create a chart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (a = 0; a <= (0.5 * r); a += increment) {
	ax = x - r; // x co-ordinate
	ay = (y + 0.5 * r) - a; // y co-ordinate

	distance = sqrt((ax * ax) + (ay * ay));

		if (distance < 35 || distance > 165) {
			cout << "Values entered are out of range!" << endl;
			system("PAUSE");
			return 0;
		}

	myfile << ax << ", " << ay << endl;
        }


I have around 8-9 of similar for loops with some doing curved lines and some doing horizontal lines. Trouble is I'm using a lot of repetitive code and would much prefer if I could create a function to do essentially the same thing.

I attempted this but the following code I came up with did not work.

1
2
3
4
5
6
7
8
9
10
int min_max_range (int x, int y) {

	distance = sqrt((x * x) + (y * y));

		if (distance < 35 || distance > 165) {
			cout << "Values entered are out of range!" << endl;
			system("PAUSE");
			return 0;
		}
}



I recommend that you return a bool instead of the int. Also what happens if the if statement doesn't execute? What value is returned then. You should always return a value from a function that returns a value.

If I change to bool instead of int, then will I still be able to use return 0?
Yes. A bool can be one of two values, zero or not zero.

Okay so now if I enter values that are out of range it will stop the program and display the error message. However now if I enter values I know are in range it won't output anything either.

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
bool min_max (double xx, double yy) {

	double distance;

	distance = sqrt((xx * xx) + (yy * yy));

		if (distance < 35 || distance > 165) {
			cout << "Values entered are out of range!" << endl;
			system("PAUSE");
			return 0;
		}
	return 1;
}

int main () {

for (a = 0; a <= (0.5 * r); a += increment) {
	ax = x - r; // x co-ordinate
	ay = (y + 0.5 * r) - a; // y co-ordinate

        //I call the function with the following
        return(min_max (ax, ay));

	myfile << ax << ", " << ay << endl;
        }
}
Last edited on
1
2
     //I call the function with the following
        return(min_max (ax, ay));

When a program encounters a return statement it returns immediately to the calling function. In this case since this line is in main the program will end immediately.

When a program encounters a return statement it returns immediately to the calling function. In this case since this line is in main the program will end immediately.


I'm not sure I fully understand that. So what your saying is that return(min_max(ax, ay)) wont even execute any code in the function, it will just recognize the return() statement and end the program regardless?
No, the function will execute then your program will try to return the value returned from the function to the operating system. Any statement following that return() statement will not execute.

Code for function "min_max" along with it's arguments/parameters in the return statement shall be executed. How else can you return a return value of "min_max" if you don't execute the function?
Last edited on
So if the values are out of range, the if statement executes, and will return 0 therefore return(min_max(ax, ay)) is the same as return(0)

However if the values are in range the if statement won't execute and instead will return 1 so return(min_max(ax, ay)) is the same as return(1) ........am I correct in thinking this??
Yes.
Throw an exception, and execute the function in a try catch loop.
This is a classic case where an exception is useful, because you don't want your input function to give an error because of out of range values.
See here:
http://www.learncpp.com/cpp-tutorial/152-basic-exception-handling/
closed account (zb0S216C)
xkcd83 wrote:
"Throw an exception, and execute the function in a try catch loop.
This is a classic case where an exception is useful, because you don't want your input function to give an error because of out of range values.
"

Wow, exception abuse much... Exceptions are meant to be used in exceptional situations, such as when a function needs to return a reference to some object but cannot return a valid object. Here's an example of such a situation:

1
2
3
4
5
6
int main( )
  {
    std::vector< int > Vector_( 5, 0 );
    std::cout << Vector_.at( 6 ); // Will throw an exception
    return( 0 );
  }

The best thing the OP can do when a given value falls beyond the set value range is return either a Boolean value or an error flag of some sort -- exceptions are not suitable here.

Wazzak
Last edited on
Topic archived. No new replies allowed.