# I'm having some trouble

This topic is 3867 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know that this is terribly inefficent but I've only been doing this for 3 weeks so please bare with me. Basically what I'm trying to do is write a simple program to find the third side of a triangle using a squared plus b squared equals c squared. It's due on monday and I can't figure out how to get the sqrt function to work. Does anyone have any insight, it would be much appreciated thank you! #include <iostream> #include <cmath> using namespace std; int main() { //declare variables (it is good practice to set these to 0 value) int num1, num2; int answer; double sqrt; //perform some math and set the last variable value cout << "This application finds the third side to any triangle.\n\n" ; cout << "Enter a whole number for side A.\n" ; //input sets the value of a variable cin >> num1; cout << endl; cout << "Enter a whole number for side B.\n" ; //input sets the value of a variable cin >> num2; answer = num1 * num1 + num2 * num2; cout << "Answer= " << answer << endl; //Attempting to find sqrt to display side c's true length cout << "Please re-type answer and press the enter key.\n" ; cin >> sqrt; cout << "Side C= " << sqrt << endl; cin.get () ; cin.get () ; return 0; }

##### Share on other sites
cmath, which you have included contains a function called sqrt which you might need to calculate the length of the third side.

If you have tried that and it didn't work, it is because you also have declared a variable with the same name, thus shadowing the function name.

You can change the name of the sqrt variable to something else, or prefix calls to the sqrt function with std:: to make it visible again.

//declare variables (it is good practice to set these to 0 value)int num1, num2;int answer;double sqrt;

Minor nitpick, what's the purpose of the comment if you don't follow the advice given there?)

##### Share on other sites
if ever you don't understand how a particular function works, you need to look up it's documentation.

for this example, just google "sqrt c++"

first result:

http://www.cplusplus.com/reference/clibrary/cmath/sqrt.html

sqrt takes one parameter (square root of) and returns the answer

float sideCLength;float sideA = 0.3f;float sideB = 0.4f;sideCLength = sqrt(sideA*sideA + sideB*sideB);

on a general maths theory note you might want to emphasise the fact that your program calculates the hypotenuse of a right angled triangle

##### Share on other sites
Quote:
 Original post by visitorMinor nitpick, what's the purpose of the comment if you don't follow the advice given there?)

Good question, I forgot thanks for pointing it out.

##### Share on other sites
Thanks guys this is very helpful

##### Share on other sites
Quote:
 Original post by toneitalianI know that this is terribly inefficent

I have no idea what you even think the word "inefficient" means here.

Quote:
 but I've only been doing this for 3 weeks so please bare with me.

I know we're on the internet, but I'd still rather not take my clothes off. In any case, the For Beginners forum is at the top of the list of forums for a reason.

Quote:
 It's due on monday

We can't do your homework for you, sorry.

Quote:
 and I can't figure out how to get the sqrt function to work.

Do you know what a function is? Do you know how to use functions in general? Do you understand what each existing line in your program does? (In detail? Have you thought to question any of these weird words like 'cin' and 'double' or are you just being fed "magic words" by the teacher?

Quote:
 Does anyone have any insight, it would be much appreciated thank you!

Many of us have quite a bit of insight here, yes.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 30
• 16
• 11
• 10
• 11
• ### Forum Statistics

• Total Topics
634113
• Total Posts
3015591
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!