Calling a fuction

I am trying to write a program that calls a another program to round a given a number. The program needs to display one of three possibilities based of the choice of the user. I have tried a variety of different ways, at first I tried to to write the called function as a void return, tried some random voodoo after that. Any help you guys could give would be really appreciated.

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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// prototype
double floor(double);
int main()
{
	cout << floor(double x, double y, double z);
	return 0;

}
double floor(double x, double y, double z)
{
	int ans2;
	double ans;
	cout << "Enter the number that you would like to have rounded: ";
	cin >> ans;
	x = floor(ans * 10 + .05) / 10;
	y = floor(ans * 100 + .05) / 100;
	z = floor(ans * 1000 + .05) / 1000;
	cout << "What would you like to round the number to?\nEnter one for one decimal point.\n" << "Enter two for two decimal points.\n" << "Enter three for three decimal points.\n";
	cin >> ans2;
	if (ans2 = 1)
		return cout << x;
	if (ans2 = 2)
		return cout << y;
	if (ans2 = 3)
		return cout << z;
	


}
The prototype (line 6) does not match the definition (line 13). Also, you are not calling the function properly (line 9). See if this tutorial helps:
http://www.cplusplus.com/doc/tutorial/functions/
Have you tried making the floor function a void function and getting rid of the if statement in place of a switch. Then within the switch statement print the answer for each different choice.
In the main program you would just need to call the floor() function.


1
2
3
4
5
6
7
8
switch(ans2){
case 1:
//equation goes here...
case 2:
//....
case 3:
//.....
}

I fixed the prototype ,but I am still getting an error on line 9. I read through the link that you gave me, and I believe that my problem has something to do with pass by reference, but I do not know how to fix it.
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// prototype
double floor(double x, double y,double z);
int main()
{
	floor(double x, double y, double z);
	return 0;

}
double floor(double x, double y, double z)
{
	int ans2;
	double ans, ans3;
	cout << "Enter the number that you would like to have rounded: ";
	cin >> ans;
	x = floor(ans * 10 + .05) / 10;
	y = floor(ans * 100 + .05) / 100;
	z = floor(ans * 1000 + .05) / 1000;
	cout << "What would you like to round the number to?\nEnter one for one decimal point.\n" << "Enter two for two decimal points.\n" << "Enter three for three decimal points.\n";
	cin >> ans2;
	if (ans2 = 1)
		return x;
	if (ans2 = 2)
		return y;
	if (ans2 = 3)
		return z;
	


}
I came up with two separate codes that come close, but they do not round the number. This is a vast improvement or not even building. Here are the two different ideas that I was thinking about.
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>
#include <iomanip>
#include <cmath>
using namespace std;
int ans2;
double ans, ans3;
// prototype
double floor(double x, double y,double z);
int main()
{
	cout << "Enter the number that you would like to have rounded: ";
	cin >> ans;
	cout << "Enter the number for the amount of decimal places you would like,\n(up to three decimal places.)\n";
	cin >> ans2;
	floor (ans2);
	return 0;

}
double floor(double x, double y, double z)
{
	if (ans2 = 1)
		return x = floor(ans * 10 + .05) / 10;
	if (ans2 = 2)
		return y = floor(ans * 100 + .05) / 100;
	if (ans2 = 3)
		return z = floor(ans * 1000 + .05) / 1000;
}


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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int ans2;
double ans, ans3;
// prototype
double floor(double x, double y,double z);

void floor(int ans2)
{
	cout << "Enter the number that you would like to have rounded: ";
	cin >> ans;
	cout << "Enter the number for the amount of decimal places you would like:\n(up to three decimal places.)\n";
	cin >> ans2;
	if (ans2 = 1)
		 ans3 = floor(ans * 10 + .05) / 10;
	if (ans2 = 2)
		ans3 = floor(ans * 100 + .05) / 100;
	if (ans2 = 3)
		ans3 = floor(ans * 1000 + .05) / 1000;
	
	return;
}
int main()
{
	
	floor (ans2);
	cout << ans3;
	return 0;

}
In the first snippet, you are passing only one parameter on line 15 when you defined the function to take three. Also, your comparisons on lines 21, 23, and 25 need to use ==, not = as that is assignment.

Why is that function even taking that many parameters? I'd think it would only need the number to round and the number of places to round to. I'd take another look at the tutorial I gave you to try to understand how functions actually use their parameters.

In the second snippet, you still have the prototype saying the function takes three parameters and you make the same mistake on lines 16, 18, and 20 as before. Your floor function here is taking a parameter it isn't even using; you should probably tone down your use of global variables (preferably to 'not at all') as otherwise they are not doing anything meaningful.
Topic archived. No new replies allowed.