falkTX wrote:Actually I still don't know why I should write 'return 0', but it seems to work
Well the basic is to know what means "return 0".
Your main is declared as INT main (), that means it needs to return something. If you want your main doesnt return anything you can just use VOID main().
The INT main usually returns a 0 if the program has completed all its task in a normal way, if the main returns a 1 means an Error has happened and it didnt complete the task(i.e: CHA1() or CHA2() didnt finish because the user didnt introduce a correct value).
So you have (if you want your main return something) to control which value is your INT main returning.
- Code: Select all
int main()
{
CHA1();
if (result[0] == 1)
CHA2();
if (result[1] == 1)
CHA3();
else
return 0;
}
Lets begin reading the code...the code begin executing CHA1(),if the user introduce a correct value then result[0]=1, now in the main we check it in the first if, it is correct so CHA2() is executed, when CHA2() is executed correctly then result[1] is equal to1 so the second if is correct and we execute CHA3(). If CHA2() was executed incorrectly(the user didnt provide a number between 3 and 80) then result[1] isnt 1 so the else catch this situation and return 0.
But "INT main" isnt returning anything if the USER has made ALL CORRECTLY
BTW you are trusting in just one "return 0" to catch a fail in CHA1() or CHA2()...that is not a good thing.why?
IF CHA1() fails ...you will check the first IF ,then the second IF and then you will return 0. Your program is executing code to arrive to the return 0.
Remember that when you arrive to a return automatically the app returns the value and STOPS the execution.This is good to prevent checking and checking to arrive to the end. Sure you can fix this too
Also you can return anything: 0,1,2,3,4 of course an INT since your main is INT main. Why returning different values?to know which CHA() was the erroneus one...if CHA1() fails you could return 1 if the CHA2() fails then you can return 2
A Logical Diagran tree is an useful Diagram to follow from top to botton the execution of code.It represents all the posibilities in a graphic, so you can control any problem of erroneus way of work really easy.


