@giblit:
Yes, I see that your code returned 1.
Also, I know that it will stop the function once it hits return.
What I meant was that how will we check what the main has returned so that we can perform different tasks for different return value.
1 2 3 4 5 6 7 8
|
int main()
{
int a = 2;
if( a == 2 ) return( 1 );
a = 3;
if( a == 3 ) return( 2 );
return( 0 );
}
|
In the above code, main will return 1 if (a==2) ==> true
or main will return 2 if (a==3 && a!=2) ==> true
Otherwise, main will return 0.
But, suppose I want that if main returns 2, then do a specific task( for instance let just print "hello world" on the console window ).
So how will I check if main has returned 2;
More clarification to what I am asking
-----------------------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
using namespace std;
void check_return_value(); // Prototype
int main(){
check_return_value();
int number = 0;
cout << "Enter a no. " << endl;
cin >> number;
if(number == 1){
return 1;
}else{
return 0;
}
}
void check_return_value(){
if(main() == 1){
cout << "Hello World" << endl;
}else{
cout << "main() did not return 1";
}
}
|
I want that if main() returned 1, then print "hello world", otherwise print "main() did not return 1".
Now, how am I supposed to check if main() returned 1 or 0. Since, I cannot call main() within any other function to check if it has returned 1 or 0. (The above code will show error since I am calling main() within other function to check what it has returned, which is not possible)
I hope you understand what I am trying to say.
Thank you,
-Himansh