I made a simple program to test whether I am able to call main in any other function like we call different functions in other function.
Therefore, I made a program to generate the maximum number by getting input of 3 numbers from the user. For this I made the function containing if else. Surprisingly (especially for me) it worked. I got that I am keep inputting the numbers then I made another variable to control the inputs. I made it to input 3 times. I got answer from bottom to end and 3 answers are generated.
#include <iostream>
using namespace std;
int main();
void max(int a, int b, int c,int p);
int p = 0;
int main()
{
p++;
int a, b, c;
cout << "\n\n";
cout << "Enter 1st number :\t";
cin >> a;
cout << "Enter 2nd number :\t";
cin >> b;
cout << "Enter 3rd number :\t";
cin >> c;
max(a, b, c, p);
cout << "\n\n\n";
system("pause");
return 0;
}
void max(int a, int b, int c,int p)
{
if (p < 3)
{
main();
}
if (a > b&&a > c)
{
cout << a << " is maximum";
}
else if (b > a&&b > c)
{
cout << b << " is maximum";
}
else
{
cout << c << " is maximum";
}
}
The output was as follows: -
Enter 1st number : 12
Enter 2nd number : 14
Enter 3rd number : 15
Enter 1st number : 45
Enter 2nd number : 69
Enter 3rd number : 88
Enter 1st number : 14
Enter 2nd number : 20
Enter 3rd number : 11
20 is maximum
Press any key to continue . . .
88 is maximum
Press any key to continue . . .
15 is maximum
Press any key to continue . . .
I don't get the logic behind it. I used Visual Studio 2017 for this.