I made a program based on function overloading consisting of 2 functions int cube (int) and float cube(float) . My main function reads values of int x and float y respectively.Now when I run the program I put a float value instead the integer va;ue And when I put 2.5(decimal value) instead of integer value in variable "x" , The compiler does not ask me the value of y and automatically 2 for x (int) and 0.5 for y (float) and returns the cube of 0.5. Why this is happenning. Why 0.5 is automatically stored in y instead of asking input?
My program is like that -
#include<iostream>
using namespace std;
int main()
{
int x;
float y;
int cube(int);
float cube(float);
cout<<"Enter a number to find its cube"<<endl;
cin>>x;
cout<<"The cube of the number "<<x<<" is "<<cube(x)<<endl;
cout<<"Enter a number to find its cube"<<endl;
cin>>y;
cout<<"The cube of the number "<<y<<" is "<<cube(y)<<endl;
return 0;
}
int cube (int num)
{
return num*num*num;
}
float cube (float num)
{
return num*num*num;
}
Output is -
Enter a number to find its cube 2.5 The cube of number 2 is 8 Enter the number to find its cube The cube of number 0.5 is 0.125
Can anyone help me about that Thanks