Hi, so I am building a calculator with extra features such as checking if a number is prime and finding a gcd of 2 numbers. So anyway, I decided to write a function for each of these, this is an example of a function to find gcd:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
long double gcd(double x, double y)
{
if ((cin.fail() == true) || (x < 1) || ((x - floor(x)) != 0) || (y < 1) || ((y - floor(y)) != 0))
{
return 1.5;
}
x3 = x2 = x;
y3 = y2 = y;
while (x2 != 0 && y2 != 0)
{
if (x2 > y2)
{
x2 %= y2;
}
else
{
y2 %= x2;
}
}
return y2+x2;
}
|
By the way, x3 and y3 are the outputs in the main function, since I can't use y2 and x2. Also can't use x and y, because if a number I entered is a large integer like 626541657 it returns it as 6.265417e8, loosing precision and looking stupid.
As you can see, the answer can only be an integer, so I used a double to return 1.5 to the main function, and if it gets such a value, it ends the program:
1 2 3 4
|
if (gcd(x, y) == 1.5)
{
return 0;
}
|
Where I ran into a problem is when I wanted to do the same for addition, since I made it so you can add double numbers too, let's say 2.4+3.2=5.6, I can't return a double value to the main function so it knows there's an error and quits. Is there a way I can I stop the program while executing a function I called from main function without returning a value to the main function?
If I return the value 1.5 to the main and tell it to stop if it gets that and I add 1 and 0.5, the program quits. Same goes for any double number/integer.
Tl;dr Is there a way to do the same as
return 0;
in the main function, just while executing a function I called from the main function?
Thanks!