OK, so I'm learning about overloading funtions to a very basic level. My understanding of it is that multiple functions can be given the same identifier but a different definition. When the function is called the compiler analyses the variables in amount and type and matches those variables to the correct function so that the output is of the right type.
I wrote a very basic program that asks for the user to input two values which are then summed with an add function. The program requires that the variables be either of int or double type and yet at runtime, if a number with a decimal is written when the first value is requested, the second level of input is skipped and the output displays a very long negative number. I can't figure out where I've gone wrong as it all seems to be correct as per my understanding. If someone could point out my mistake to get the program working I would appreciate it greatly.
#include <iostream>
int add(int a, int b)
{
return a+b;
}
double add(double a, double b)
{
return a+b;
}
int main(void)
{
using std::cout;
using std::cin;
int number1;
int number2;
cout << "This program will sum two numbers and return the value with\nthe correct data type.\n";
cout << "Please enter the first number to be summed: ";
cin >> number1;
cout << "\nPlease enter the second number to be summed: ";
cin >> number2;
cout << "\n\nThe answer is: " << add(number1, number2) << "\n\n";
return 0;
}