Please i want to end a program in a function outside
function main.
also want to end executing a function of type void without
allowing it to end but don't know how.
please help me out.

Recommended Answers

All 15 Replies

Please i want to end a program in a function outside
function main.

All functions run in main(), since it's the entry point of the program. No user-defined function can run outside main(). Once main() ends, all other functions end.

Please i want to end a program in a function outside
function main.

Is exit what you want?

also want to end executing a function of type void without
allowing it to end

Not sure what "it" is at the end there... if you want to exit a void function, a simple return statement should do.

also want to end executing a function of type void without
allowing it to end but don't know how.

Stick return; where you want it to end.

No user-defined function can run outside main().

Assuming you mean that no user-defined function can run before main (ignoring that you seem to have misunderstood the OP), consider this:

#include <iostream>

class foo {
public:
    foo() { std::cout << "Me first!" << std::endl; }
} obj;

int main()
{
    std::cout << "In main()" << std::endl;
}
commented: Whenever I read your post, I always learn something new. +2
commented: Can't resist adding reps... +0

Assuming you mean that no user-defined function can run before main

That was not what I meant. In my own opinion, the OP meant that a user-defined function can run AFTER main() has ended, of which I try to explain that if main() has ended, the program has ended.

@capton:
Why would you want to do that?
The language is arranged for a particular flow that you will be interrupting.
Would it be possible for your design to adhere to the natural flow?
Just return control to main -- maybe with a flag that tells main() it's time to exit?

That was not what I meant. In my own opinion, the OP meant that a user-defined function can run AFTER main() has ended, of which I try to explain that if main() has ended, the program has ended.

Even that is wrong. Add a

~foo() { std::cout << "Me last!!" << std::endl; }

to Narue's example.

@capton:
Why would you want to do that?
The language is arranged for a particular flow that you will be interrupting.
Would it be possible for your design to adhere to the natural flow?
Just return control to main -- maybe with a flag that tells main() it's time to exit?

Yeah! That makes sense! It might be a project assigned to him. Still.....

thi is the code and it's for a quadratic eqn problem.
please help me out.

#include<iostream>
#include<cmath>
using namespace std;
void check_fxn();
float a,b,c;
float d= pow(b,2)-(4*a*c);//determinant
int main()
{
cout<<"enter values \n ax^2 + bx + c = 0 "<<endl
    <<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
cout<<"c = ";
cin>>c;
check_fxn();
float x1=(-b-sqrt(d))/(2*a);
float x2=(-b+sqrt(d))/(2*a);
cout<<x1<<endl<<x2;
return 0;
}
void check_fxn()
{
if(a==0)
;// end program ?
else if(d<0)
{
cout<<"roots are imaginary"<<endl;
//end program ?
}
else
;//end function ?
}

With what?
Formatting? OK, here.
Stopping the program from inside the function? Don't. It's a bad practice. Return a value that indicates what to do in main()

Use exit(0). In your app, it exits after the enter key is pressed down.

#include<iostream>
#include<cmath>

using namespace std;
void check_fxn();
float a,b,c;

float d= pow(b,2)-(4*a*c);//determinant
int main()
{
	cout<<"enter values \n ax^2 + bx + c = 0 "<<endl
		<<"a = ";
	cin>>a;
	cout<<"b = ";
	cin>>b;
	cout<<"c = ";
	cin>>c;
	check_fxn();
	float x1=(-b-sqrt(d))/(2*a);
	float x2=(-b+sqrt(d))/(2*a);
	cout<<x1<<endl<<x2;
	return 0;
}

void check_fxn()
{
	if(a==0)
	exit(0);// end program ?

	else if(d<0)
	{
		cout<<"roots are imaginary"<<endl;
		exit(0);//end program ?
	}
	else
	return;//end function ?
}

produced an error in line 28 and 33

'exit' was not declared in this scope

You need to include stdlib(.h?)

Thanks.
It worked

Just use a ExitProcess() in another procedure, or in the last action of main() you use a GoTo to another procedure

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.