Hey all,

I'm new to C and trying to pick up good habits before I get too routed in my ways.

Essentially, I have a code which runs a mathematical test on an array, and depending on the percent error, it decides to continue iterating or stop. Essentially, I have a function nested in another function.

float threshold

void main()
{

threshold = 1.0

math(stuff)
}

float math(float stuff)
  {   
   float error;
    Do more stuff
   error= experimental_error(values)
  }

float error(values)
  {
    Calcualate experimental error
  }

What I'd like the program to do is check whether the experimenal error is below a global variable, threshold. Once this condition is satisfied, I want to immediately exit the program. Is there a command in C which essentially ceases the entire program? In python, there is a command sys.exit(1). I realize that I can break out of the inner loop, but then I'd have to break out of the other loops too, correct? I'd have to break out of the error function, then the math function, then the main function. I don't want to resort to a series of breaks, and conditions therein. Any ideas for how to breakout from deep within a code?

Recommended Answers

All 3 Replies

return; should work

use exit(1); to exit from the main program.

else

use return (1);

First off, use int main(), not void main()!

At the end of your main() you should have return 0;

Also note that using globals is generally a bad idea. It makes the program less moduler.

Your functions also don't return anything, so you can declare them as voids. Looking at your code, it seem's your using functions as a replacement of "goto".

I don't recommend it, but exit(0) should be able to exit your program at any point.
I recommend you use a while loop to keep running your code until a condition is met.

Good luck programming :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.