breaking out of main()

Started by
9 comments, last by Antheus 16 years, 1 month ago
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?
Advertisement
sorry guys i foudn it :) exit(0);
Or, you could 'return', if that is the main function there.

(PS. main() should really return an int, not a void.)
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.
C++ Faq lite.

The return value is passed to whatever originally called the program (usually the OS), and indicates how it terminated. 0 is usually used to indicate successful termination.
cheers tang, i kinda understand it now.

still dont knwo why kylotan would use an int instead of void xD
Quote:Original post by 15Peter20
still dont knwo why kylotan would use an int instead of void xD


C++ Faq lite.
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??????
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.
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 :)

This topic is closed to new replies.

Advertisement