Jump to content
  • Advertisement
Sign in to follow this  
toneitalian

I'm having some trouble

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by visitor
Minor 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 this post


Link to post
Share on other sites
Quote:
Original post by toneitalian
I 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!