Problem with overlapping of functions with same name

Ok, so this is the question I am trying to do,

Write two functions having same name Add and takes two arguments. When
called from main, they will print “I have been called n times” where n is the
number of calls, they have received from main, and will return the sum of
arguments passed.

And the following is the code I made for this program.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;
int add(int, int, int &, int &);
double add(double, double, int &, int &);
int main()
{
	double a, b, d; 
	int x = 0, y = 0; 
	char c;
	bool fh = true;
	
	while (fh)
	{
		cout << "Enter first number: ";
		cin >> a;
		cout << "Enter second number: ";
		cin >> b;
		d = add(a, b, x, y);
		cout << endl << "Sum = " << d; 
		cout << endl << "Would you like to add another numbers? " << endl << "Type Y for yes: ";
		cin >> c;
		cout << endl; 
		if (c == 'y' || c == 'Y')
			fh = true;
		else
			fh = false; 
	}
	cout << endl;
	system("pause");
	return 0;
}
int add(int a, int b, int &x, int &y)
{
	int c; 
	x++;
	cout << "I have been called " << x << " times" << endl;
	c = a + b;
	return c;
}
double add(double a, double b, int &x, int &y)
{
	y++;
	double c;
	cout << "I have been called " << y << " times" << endl;
	c = a + b;
	return c;
}


Program runs fine, however, I am getting one big issue here... For what I know, in this program, now if user enters a number without decimal then the program is supposed to take that as integer and take it to the function with integer parameters. But it is not doing that... It is taking it to function with double parameters instead..
To resolve it, I tried to change a and b in main function to integers however, that didn't work and resulted in program running for infinite time.

Is there any possible way to solve it? How can I make the program to take the variables a and b to the parameters to double function only when I enter decimal value in one of these 2 variables? Thank you!

(Just for example, this is the output I am getting right now)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Enter first number: 2
Enter second number: 3
I have been called 1 times

Sum = 5
Would you like to add another numbers?
Type Y for yes: y

Enter first number: 3
Enter second number: 2
I have been called 2 times

Sum = 5
Would you like to add another numbers?
Type Y for yes: y

Enter first number: 2.13
Enter second number: 4
I have been called 3 times

Sum = 6.13
Would you like to add another numbers?
Type Y for yes:
Add and add are different names in C++, which is case sensitive. Also, your functions take 4 arguments, not two.

It's not going to call a function that takes an int if the variable you're using is a double. It doesn't matter what it's value is or whether or not it has a fractional part. You need to use ints to call the function that takes ints.

So maybe you can read your input as a string. If it has a decimal point then set a couple of doubles and call Add with them. Otherwise, use ints.
Don't create variables uneccesarily. The following is less code without any sacrifice to readability.
return a+b;

Are you familiar with the 'static' keyword?

Instead of this:
1
2
3
4
5
int add(int a, int b, int &x, int &y)
{
	cout << "I have been called " << ++x << " times" << endl;
	return a+b;
}


do this:
1
2
3
4
5
6
int add(int a, int b)
{
	static int x = 0;
	cout << "I have been called " << ++x << " times" << endl;
	return a+b;
}


The variable 'x' will be initialized to 0, and maintain it's value the next time the function is called. It behaves like a global, but with limited scope.

The task you're trying to accomplish seems kind of strange, but the easiest way I can think of to do it would be to read the input as a string, check if there is a decimal in either number and then convert the strings to the appropriate type before calling Add();
Last edited on
When you read a double, you read a double.

When you (try to) read an int but the stream has something else than int, for example a dot, the stream goes into failed state. (Reading to double will fail for all non-float inputs too.)


If your input has at most whitespace, digits, dot (and possibly prefix - or + on "numbers), then this could work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
  std::string foo;
  std::cin >> foo;
  if ( std::string::npos != foo.find( '.' ) ) {
      double bar = stod( foo );
      std::cout << bar;
  }
  else {
      int bar = stoi( foo );
      std::cout << bar;
  }
}

Check http://www.cplusplus.com/reference/string/ to know what I did there.
Hello redfury,

now if user enters a number without decimal then the program is supposed to take that as integer and take it to the function with integer parameters.
No. Using "cin>>a" is formatted input. since a is defined as a double this input is taken as a "double" and when the function is called it will choose the function that takes the "double"s.

To solve the problem my first thought would be to create two "int" variables and use an if statement to decide which type of variable to use then it will call the proper function based on the first two parameters.

I noticed that you are sending both "x" and "y" to each function. This works, but each function used only one of these variables, so you only need to send one.

Hope this helps,

Andy
Topic archived. No new replies allowed.