Issues w/ function call "larger" WOW WAS IT SOMTHING I SAID?


What I'm trying to do here is figure out the greatest of 3 numbers (using the value returning function "larger". Then assign that value to the variable
"side_c_hypotenuse".



Anyway, I highlighted the area in the program where I'm having the problem down near the end.
(I'm sure it's all screwed up)...




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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>


using namespace std;

double larger (double x, double y);

int main ()
{
	string user_Initials;
	double n1, n1_b,
	n2, n2_b, n3, n3_b, max;
	double side_A,                                      
	side_B,                                      
	side_C_Hypotenuse;
	
	
	
	cout << "Enter user initials: " << endl;
	getline (cin,user_Initials);
	
	cout << "Hello, this program determins type of triangle" << endl;
	cout << "by evaluating the length of three sides." << endl;
	
	cout << "Enter first number (positive numbers only)..." << endl;
	cin >> n1;
	max = n1;
	
	
	if (n1 <= 0)
	{
		cout << "Invalid Entry! Input must be a number greater than zero" << endl;
		cout << "Re-enter first number..." << endl;
		cin >> n1_b;
		cout << "Thank you!";
		n1 = n1_b;
		max = n1;
	}
	
	else
	{
		cout << n1 << " is within range..." << endl;
		cout << "Thank you!" << endl;
	}
	
	
	cout << " Enter second number..." << endl;
	cin >> n2;
	max = larger(max, n2);
	
	if (n2 <= 0)
	{
		cout << "Invalid entry! Enter a number GREATER than zero." << endl;
		cout << "Re-enter second number..." << endl;
		cin >> n2_b;
		cout << "Thank you!";
		n2 = n2_b;
		max = larger(max, n2);
	}
	
	else
	{
		cout << n2 << " is within range." << endl;
		cout << "Thank you!" << endl;
	}
	
	cout << " Enter third number..." << endl;
	cin >> n3;
	max = larger(max, n3);
	
	if (n3 <= 0)
	{
		cout << "Invalid entry! Enter a number GREATER than zero." << endl;
		cout << "Re-enter third number..." << endl;
		cin >> n3_b;
		cout << "Thank you!" << endl << endl;
		n3 = n3_b;
		max = larger(max, n3);
	}
	
	else
	{
		cout << n3 << " is within range." << endl;
		cout << "Thank you!" << endl;
		cout << endl;
		
		return 0;
	}
	
	
	
	n3 larger (n1, n2)
	{
		if (x >= y)
			return x;
		else
			return y;
		
	}
	

	
	
	side_C_Hypotenuse = max;
	side_A = n1;
	side_B = n2;
	
	
	cout << fixed << showpoint << setprecision(2);
	cout << side_A << endl;
	cout << side_B << endl;
	cout << side_C_Hypotenuse << endl;
	
    	return 0;
}
Last edited on
closed account (D80DSL3A)
Yes, you are trying to define the function larger() within the main() function. This is no-can-do. You can't define one function within another. Place the definition (your code in bold) outside the main() - ie, below what you have posted. The last } at the bottom of your post is the end of main().

Make the definition match the prototype you gave for the greater() above main.

Here's your prototype:double larger (double x, double y);
The definition should look like this:
1
2
3
4
double larger (double x, double y)
{
// the code inside your bold section
}


You also have an extra return 0; in your main(). This would cause your program to end earlier than you want.
closed account (z05DSL3A)
jyo76,

Please use code tags: [code] Your code [/code]
Hi jyo76. Yes, fun2code is quite right, you need to define the declared function "larger" outside of the main() body. You also need to make sure that the function declaration is replicated at the top of the corresponding function definition - although in the declaration you don't need to specify parameter names (i.e. x and y), but you do in the definition.

Also, yes, the extra return 0 will terminate the program prematurely and is not needed.

You might also want to call an additional predicate function when you test for the negative/zero entries to avoid code replication. Lastly, it might be a good idea to throw exceptions after negative/zero entries, or incorporate a do - while loop that calls this predicate function just in case the client enters a zero/negative number twice in a row.

Hope that helps, good luck!



jyo76 wrote:
Silly question (judging from all the replies), but any help/advice would be greatly appreciated.

No one ever said or even implied this. Why are you offended?
Last edited on
closed account (zb0S216C)
If you're trying to compare three integers or floating-points for the highest value, you could do the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
double Get_larger( double X, double Y, double Z )
{
    // Create a tempory buffer, which is of type double, and store X in it. 
    // Then, Compare Y and Z to the buffer. If Y or Z is greater than
    // that of X, place the largest into the buffer. 
    double Buffer( /* Place X into this buffer */ );

    // If Y is greater than the value in the buffer, then...
    {
        // Update the buffer...
    }

    // If Z is greater than the value in the buffer, then...
    {
        // Update the buffer...
    }

    // When finished, return the largest( buffer ). 
}

This is a rough idea, but, you get the idea of how to do it.
Last edited on
thanks everybody..

that was all great info...

but, is it possible to do the same thing without using functions?

could I use if/else statements alone?
Sure it's possible to doing without functions, but why not use everything you have at your disposal? The longer you put off learning something the worse off you'll be later. Trust me, I've been doing the same thing with classes. ;)
closed account (D80DSL3A)
Sure. For example, the function call made on line 52 max = larger(max, n2); could be done directly with:
if( max < n2 ) max = n2;.
I wouldn't advise you try to avoid learning about functions though. They are very valuable in more complex situations.
If you already want to avoid functions, you can put the entire functionality of Get_larger into a single (really ugly) line of code. This isn't really any more efficient though, and learning to use functions is one of the first steps you should take. Your main code should really only do the necessary things, or rather the things that are completely specific to your program. For example, writing void functions things that just cout a specific text isn't useful, nor is it really a good practice. There is no point in having such a function as it has zero reusability value. Same with basic i/o - do you really have to write functions that prompt the user to enter his name? I don't think so. What you do with the name later on is more important.
fun2code,

can you (or anyone else) elaborate on this a little?

"Sure. For example, the function call made on line 52 max = larger(max, n2); could be done directly with:
if( max < n2 ) max = n2;."


i am having trouble using this logic to place the numbers in order...

I'm not trying to avoid learning functions, my professor wants me to avoid them for the moment. (we haven't gotten to that chapter yet)!
Last edited on
It is really driving me nuts how such a simple concept is so hard (for me ) to figure out...

who would have thought that putting 3 numbers in order could be so complicated???
closed account (D80DSL3A)
OH. You should be able to just substitute if( max < n2 ) max = n2; where you have
max = larger(max, n2); and likewise where n3 is involved.
Actually it's the input collection and validation that consumes most of the code!

Using your methods just 3 lines would find the max:
1
2
3
max = n1;
if( max < n2 ) max = n2;
if( max < n3 ) max = n3;// done! 
ohhhhh, and then I can use "max" anywhere i need it!

OMG I'm stupid!

Thanks fun2code!
i ended up w/ somthin like this...

i had to do some messy things to get the middle number to end up in the right place as "side_B"...




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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
cout << "Enter first number (positive numbers only)..." << endl;
	cin >> n1;
	max = n1;
	min = n1;
	
	
	
	
	
	if (n1 <= 0)
	{
		cout << "Invalid Entry! Input must be a number greater than zero" << endl;
		cout << "Re-enter first number..." << endl;
		cin >> n1_b;
		cout << "Thank you!";
		n1 = n1_b;
		max = n1;
		min = n1;
	}
	
	else
	{
		cout << n1 << " is within range..." << endl;
		cout << "Thank you!" << endl;
	}
	
	
	
	cout << " Enter second number..." << endl;
	cin >> n2;
	if (max < n2) max = n2;
	if (min > n2) min = n2;
	
			
	
	if (n2 <= 0)
	{
		cout << "Invalid entry! Enter a number GREATER than zero." << endl;
		cout << "Re-enter second number..." << endl;
		cin >> n2_b;
		cout << "Thank you!";
		n2 = n2_b;
		if (max < n2) max = n2;
		if (min > n2) min = n2;
	}
	
	else
	{
		cout << n2 << " is within range." << endl;
		cout << "Thank you!" << endl;
	}
	
	
	
	cout << " Enter third number..." << endl;
	cin >> n3;
	if (max < n3) max = n3;
	if (min > n3) min = n3;	
	if (n1 > n2 && n1 < n3) n2 = n1;
	if (n2 > n1 && (n2==max)) n2 = n1;
	if (n2 < n3 && (n3!= max)) n2 = n3;
	

	
	if (n3 <= 0)
	{
		cout << "Invalid entry! Enter a number GREATER than zero." << endl;
		cout << "Re-enter third number..." << endl;
		cin >> n3_b;
		cout << "Thank you!" << endl << endl;
		n3 = n3_b;
		if (max < n3) max = n3;
		if (min > n3) min = n3;	
		if (n1 > n2 && n1 < n3) n2 = n1;
		if (n2 > n1 && (n2==max)) n2 = n1;
		if (n2 < n3 && (n3!= max)) n2 = n3;
		
	}
	 
	else
	{
		cout << n3 << " is within range." << endl;
		cout << "Thank you!" << endl;
		cout << endl;
	}
	
	
	
	side_A = min;
	side_B = n2;
	side_C = max;
closed account (D80DSL3A)
That is a LOT of code for that! Figuring that out must have been a brain buster! Does it work?
You could reduce it a lot by doing all the sorting at the end.

Start by just getting the values for n1, n2 and n3. Eliminate the "transfer" variables nx_b.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
cout << "Enter first number (positive numbers only)..." << endl;
	cin >> n1;	
	
	if (n1 <= 0)
	{
		cout << "Invalid Entry! Input must be a number greater than zero" << endl;
		cout << "Re-enter first number..." << endl;
		cin >> n1;
		cout << "Thank you!";		
	}	
	else
		cout << n1 << " is within range..." << endl << "Thank you!" << endl;

All 3 inputs could be taken this way.
Note that if the user entered an invalid value the 2nd time we are taking it. Correcting this will have to wait until you know about while loops though.
Repeat this for n2 and n3 (arrays and a for loop would come in handy here).

Then do the sorting.
I'll suggest another method. Call it a mini "bubble" sort.
1
2
3
4
5
6
7
8
int t=0;// swap "buffer"
if( n1 > n2 ){ t=n1; n1=n2; n2=t; }// swap n1 with n2
if( n2 > n3 ){ t=n2; n2=n3; n3=t; }// swap n2 with n3 - max is now n3
if( n1 > n2 ){ t=n1; n1=n2; n2=t; }// swap n1 with n2 again - min is now n1

cout << "min = " << n1 << endl;
cout << "mid = " << n2 << endl;
cout << "max = " << n3 << endl;

That should do it!
Last edited on
Ahhhh, very nice!

I like your "mini bubble sort" SO much more than my "rats nest of random happenstance"!!
(it does work), (for the most part) lol... But, if I happen to enter a combination of a Small number, followed by two larger numbers of equal value, (4,6,6) , the program returns (4,4,6). Every other possible combination of numbers work out as intended!
Yes, my brain was busted! Seems to have been stuck in low gear :( Needless to say, I will be using the "mini bubble sort" method to solve this problem in the future (or at least until chapter 6) when my class starts learning how to use functions!
Thanks again for your guidance fun2code! You really helped me understand this concept... But I do take credit for figuring out how NOT to do it!!



Topic archived. No new replies allowed.