Sign in to follow this  
15Peter20

breaking out of main()

Recommended Posts

hello people, im making an AI demo which laods in a map.txt file and then implements a best-first algorithem. my problem is the loading in from a file here is where i do it:
string mapName; // variabel to hold name of map in
	char mapArray[20][20]; // array to store the loaded map in
	std::fstream InFile; // fstream variable to use the fstream functions
	
	// ask user for name of map
	cout<< "\nPlease enter name of map, (e.g. map1): ";
	getline(cin, mapName);
	
	mapName = "Maps\\" + mapName + ".txt";

	// open map file that user entered and store into a 2D array
	InFile.open(mapName.c_str());
	for(int x=0;x<20;x++)
	{
		for(int y=0;y<20;y++)
		{
			InFile>> mapArray[x][y];
		}
	}
	InFile.close();

	// check if file loaded correctly
	if(InFile.fail())
	{
		cout<< "\nMap has failed to load!!"<< endl;
		system("pause");
	}// end if infile fail

my problem i am having is the if(InFile.fail()) that section works however i would like it so the program breaks; out of the void main() however it keeps saying it is an invalid break. i want the program to basically stop there and dont carry on, basically exit the program. is there another way of doing this except from using break?

Share this post


Link to post
Share on other sites
firstly what would it return???

and in all my time at uni, the main has alwasy been a void never have i seen it been an int yet, could you expand on it more, and explain why you think main() should be an int and not a void???

im quite curious as too why.

Share this post


Link to post
Share on other sites
thanks tooh.

so according to the link that you gave me, using void main() is not a standard and can only be complied in 'Most' compiliers.

i will try use int main() from now on, and we shall see the outcomes.


however this got me thinking, what about the functions i call within main (and others) which are void, eg void FrontEndSetup(){...}

should these still be a void, or should all voids becomes int's to keep to standards??????

Share this post


Link to post
Share on other sites
When your OS, or another program calls your application the return value can be queried to determine how sucessfully the program exited.
A return 0 would indicate that the program ran without error, and exited normally. It is therefore strongly advised, and part of the C++ standard to ensure that you main returns an int ( C standard is slightly different. )

edit:- I was beaten to it :)

Share this post


Link to post
Share on other sites
Quote:
Original post by 15Peter20
sorry guys i foudn it :) exit(0);


Don't do that. Ever.

At one point, you'll find that you want to re-use the code, you'll copy it into some function, that will be part of some object, and then your application will be happily running, and at some point - boom. Crash?

As an example of why this is bad I'll quote Lua. Scattered in there are exit()s. Which is great, unless you happen to be running Lua as scripting tool in some large application.

A simple type-cast error will kill the application without a warning, without an error message, without anything.

So, forget that exit() exists.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this