Display function

Pages: 12
Hello there I'm new at creating and calling void functions and also the bool value. I have written this program to evaluate if a the user inputs a right triangle or not. Using the bool function, pass by reference and pass by value. Seems pretty simple but I'm not sure how to make it return the statement that it is not true if the user enters a non right angle triangle. Maybe I have the equation wrong and need to use the sqrt() function? Not sure, maybe I have some of the syntax wrong in for the DisplayResults function.
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
#include <iostream>
#include <cmath>
using namespace std;

void GetDimensions(int &, int &, int &);
bool IsRightTriangle(int, int, int);
void DisplayResults(int);

// Function main
int main()
{
	int sideA = 0;
	int sideB = 0;
	int sideC = 0;
	int yesOrNo;
		
	// Call void GetDimensions function
	GetDimensions(sideC, sideB, sideA);  

	yesOrNo = IsRightTriangle(sideC, sideB, sideA);

	DisplayResults(yesOrNo);

	return 0;
}


void GetDimensions(int &num1, int &num2, int &num3)
	{		
	cout << "Enter one side of a triangle: ";
	cin >> num1;
	cout << "Enter one side of a triangle: ";
	cin >> num2;
	cout << "Enter one side of a triangle: ";
	cin >> num3;
	}

bool IsRightTriangle(int sideC, int sideB, int sideA)
	{
		bool status;

		if (sideC == (sideB + sideA))
			status = true;
		else
			status = false;
		return status;
	}		

void DisplayResults(int results)
	{
	       if (results = true) 
		    cout << "\nYes, this is a right triangle.\n\n";
	       else 
		    cout << "\nNo, this is not a right triangle.\n\n";
	}

I get no errors running this in visual studio, but the only problem is I cannot get it to return the proper statements yes it is or no it is not, when a user enters the three numbers for the right triangle...any help would be greatly appreciated.
Last edited on
Your errors are in the math you are using in IsRightTriangle().

Also, it is scary for a user when the program does not acknowledge their input. You might want to rephrase your input function:
1
2
3
4
5
6
7
8
9
void GetDimensions(int &num1, int &num2, int &num3)
	{
	cout << "Enter the first side of a triangle: ";
	cin >> num1;
	cout << "Enter the second side of a triangle: ";
	cin >> num2;
	cout << "Enter the last side of a triangle: ";
	cin >> num3;
	}

Lastly, since the test in an if statement is itself a boolean expression, you can simply return it. That is, the following are logically equivalent:
1
2
3
4
5
6
7
8
	{
	bool result;
	if (a == 42)
		result = true;
	else
		result = false;
	return result;
	}
1
2
3
	{
	return (a == 42);
	}

Hope this helps.
Ok thanks for your reply. I completely agree with the user not knowing what he or she is entering, good catch. It's amazing when you look at something for hours and someone else looks at it once and sees it immediately. For the bool I'm a little confused. I also need to use all three functions, and return results using the DisplayResults(). So should I use the sqrt() in some way in the IsRightTriangle()? Thanks for your quick reply!!!
Your function that checks if triangle is right seems to be wrong. You need check if any side is longer than sum of other ones. If is so, the triangle isn't right
Last edited on
Thanks for help, I really appreciate it. I'm still not sure how I would use the bool return function to evaluate if the numbers that were entered for a right triangle equal a right triangle? Should this be done in the function main? Any help would be greatly appreciated since this is new to me.
I believe you're trying to using the Pythagorean Theorum to determine if it is a right triangle or not. You have two problems though.

1. The theorum itself is hyp2 = side12 + side22. You aren't squaring any of your sides so the comparison won't be valid.

2. I've used explicit labels in problem 1 to illustrate problem two. You can't just pick sides randomly and plug them into the comparison. You have to make sure the larger of the 3 sides is separated out as the hypotenuse for the test. If sideC is not the hypotenuse, the comparison won't be valid.
Yes correct Pythagorean Theorem. Should I just multiply each side by itself to square them?
No, use pow(base, exponent).

Disregard this.

-Albatross
Last edited on
Ok cool, thanks. I couldn't figure out if I should use the pow or the sqrt. I will use pow in the GetDimensions() function. I'm still trying to to figure out how to use the bool IsRightTriangle function to return true or false to function main. Any ideas? Thanks for your reply!
Well... you can use sqrt as well.

Well, why not have displayResults() take a bool, and have yesOrNo defined as a bool?

-Albatross
Thanks for this idea,
like this:
1
2
3
4
5
6
7
8
bool DisplayResults(double results)
	{
		if (results = true) 
			cout << "\nYes, this is a right triangle.\n\n";
	        else 
		    cout << "\nNo, this is not a right triangle.\n\n";
		return results;
	}

DisplayResults(yesOrNo);

The bool is new to me so I'm not sure.

No, use pow(base, exponent).

Why? x2 is the same as x*x, so unless the user types in some super huge number for a homework assignment, then there won't be any problems.

Although, I would tend to use doubles instead of ints for the lengths.

I will use pow in the GetDimensions() function.

Why? Don't mix stuff up.

GetDimensions() should only get the actual lengths of the sides from the user. It shouldn't return something else, like the squared dimensions or the like.

Keep all the math in one spot: in IsRightTriangle().

Determine which side is longest. Hint: given three sides, just make sure the one you want to be the longest is the longest. That is, if you say that sideC should be the longest, then it should be larger than sideA and sideB. Then follow the instructions given you to make it easy:

1a. if sideC < sideA then swap the values of sideC and sideA
1b. if sideC < sideB then swap the values of sideC and sideB
2. multiply each side by itself: sideA *= sideA; ...
3. return the comparison sideC == sideA + sideB

Again, all this only belongs in the IsRightTriangle() function. Other parts of your code should not be involved in the calculation.

The bool is new to me so I'm not sure.

You used it just fine on lines 38 through 47 of your first post.

The type of a thing is important. Since IsRightTriangle() returns a bool, the following should also be bools:
- The YesOrNo variable (bool YesOrNo;)
- The argument to DisplayResults() (void DisplayResults(bool results))

Hope this helps.
Last edited on
Oops. I misunderstood what he meant. I thought... eh, I don't know what I thought.

Disregard what I said.

-Albatross
Duoas, you are the man! Thank you so much for taking the time to post all this info. Your help is greatly appreciated and I love the way you explained it all, you should be a programming teacher at a college! Is this what you do for a living? Anyways I will work on this and get back with the results. Another question, should I just use double through the entire program to avoid any mixing up? ...i think i will. Thanks again for taking the time to look this over you have been the most helpful person I have found on the web so far! :=)

Real quick, not sure if I got the true and false correct, will the IsRightTriangle() look something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Use bool function to evaluate triangle
bool IsRightTriangle(double sideC, double sideB, double sideA)
	{
		bool status;

		if ((sideC < sideA) && (sideC < sideB))
		{
			swap(sideA, sideC);
			swap(sideB, sideC);
			sideA *= sideA;
			sideB *= sideB;
			sideC *= sideC;
			status = true;
		}
		else		
			status = false;				
		
		return status;
	}
You're welcome. :-)

No. Do one thing at a time.
Thanks....also, if I'm using the above and multiplying each side by itself, do I still need to use the pow(x,y) in the GetDimensions() function?
Thanks again for your help Duoas, you really helped me out, can I consult you again for future reference? :-)

I removed the pow from the GetDimensions() function and it still works just fine.
1
2
3
sideA *= sideA;
sideB *= sideB;
sideC *= sideC;

This code above will take the place of it in the IsRightTriangle(), does this sound correct? I don't think there is a need for both, one or the other should be just fine. Why would I use both?

Exactly, there is no need to use both.
Hey Duoas, since I have finished this program, and it works the way it should, and I have no more questions on this program, do I mark solved for this thread? Or should I just leave this thread as is??
If you think it has been solved, then mark it as solved. :-)
Pages: 12