Jump to content
  • Advertisement
Sign in to follow this  
15Peter20

breaking out of main()

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
Or, you could 'return', if that is the main function there.

(PS. main() should really return an int, not a void.)

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
Quote:
Original post by 15Peter20
should these still be a void, or should all voids becomes int's to keep to standards??????


No, this only applies to the main function.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!