11 Replies - 36103 Views - Last Post: 16 September 2010 - 06:55 PM Rate Topic: -----

#1 jakedog56  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 09-September 10

Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 12:51 PM

Hello! Newbie C++ student.
Trying to write a program to compute the hypotenuse of a right triangle.
I feel I am very close but I cannot see my error. As of now, the program compiles without error, runs without erroring out, but it does not display the answer. It just takes the input and then quickly closes.

Here is what I have so far:

#include <iostream>
#include <cmath>
using namespace std;
  
int main ()
{
    float A, B;
    float side1;
    float side2;
    float side3;
    // Program Starts
    
    cout << "Hypotenuse of a right triangle\n";   // Program title
    cout << "\nA^2+B^2=C^2 (Principle of the Pythagorean Theorem)\n"; //Mathmatical formula
    cout << "\nEnter Length of Side A\n\n";
    cout << "A = ";
    cin >>  A;
    cout << "\nEnter Length of Side B\n\n";
    cout << "B = ";
    cin >>  B;
  
    {
        side1=pow(A,2);
        side2=pow(B,2);
        side3 = sqrt((side1+side2)); //Compute hypotenuse of triangle
        
        cout << "\n\n Side 1 is  " <<side1;
        cout << "\n Side 2 is  " <<side2;
		cout << "\n The Hypotenuse is  " <<side3;
      
    }
	
 }


Any suggestions or comments on where I am going wrong?

Thanks in advance!

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 09 September 2010 - 12:57 PM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Hypotenuse of a right triangle- C++

#2 Splatocaster  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 51
  • View blog
  • Posts: 182
  • Joined: 22-December 09

Re: Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 12:52 PM

Add system("PAUSE"); or while (true); at the end, right before the very last }
Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 12:58 PM

This is also found in our FAQs at the top of the forum: Holding the execution window open
Was This Post Helpful? 1
  • +
  • -

#4 jakedog56  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 16
  • Joined: 09-September 10

Re: Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 01:06 PM

Ok. Thanks for the help. I think that I have it figured now.

PS: I apologize for not using the code tags. First time posting on the forum and I didn't read through the rules closely enough first. I will make sure to use them in the future!

Thanks again!
Was This Post Helpful? 2
  • +
  • -

#5 taylorc8  Icon User is offline

  • B&

Reputation: 150
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 01:29 PM

What are those curly braces doing there? You can't just slap those in there wherever you want!
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 01:33 PM

There's nothing strictly wrong with the braces where they are. They introduce a local scope that's entered every time, with variables defined therein only existing for the duration of the block.
Was This Post Helpful? 1
  • +
  • -

#7 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 178
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: Hypotenuse of a right triangle- C++

Posted 09 September 2010 - 04:43 PM

it isn't vital to the running of the program but at the end where you output side1 and side2 you should really be outputting A and B as at the moment you are saying that side 1 is the square of what it actually is.

User says 1st side is 4 you are then saying that side 1 is 16

Unless you mean this, I just think it would look odd/confusing to a user.

Or alternatively output

cout << "\n\n Side 1^2 is  " <<side1;
cout << "\n Side 2^2 is  " <<side2;
cout << "\n The Hypotenuse is  " <<side3;

Was This Post Helpful? 1
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Hypotenuse of a right triangle- C++

Posted 10 September 2010 - 05:50 AM

*
POPULAR

View Postjakedog56, on 10 September 2010 - 04:51 AM, said:

    float A, B;
    float side1;
    float side2;
    float side3;



The following will not affect how your program works but good habits are best picked up early and bad ones abandoned as quickly as possible.
Depending on your teacher you might even gain or lose a few marks in this area too so it's worth looking at.
If your teacher gave you those variable names and insisted you use them then they should know better.
Let's assume you have room to move here.

Variable naming is important.
Using good variable names that conform to accepted conventions will make your code far easier to write, read and maintain.

One
It is usually bad form to name a variable with a capital letter or starting with a capital letter.
Therefore "A" and "B" would usually be considered bad variable names.
If you really had a reason to use single letter variable names the "a" and "b" would be better.
However, as we shall see, a single letter variable name in this case is a bad choice.

Two
Meaningful variable names are important.
"A" and "B" don't mean anything.
How about "lengthSideA" and "lengthSideB" instead?

Three
Consistency and coherence is important.
Why do you accept the inputs of the lengths of the two sides as letters and then start using numbers in "side1", "side2" etc?
That's just confusing (and I think it even confused you - see the excellent comments from bonyjoe above).
How about keeping things consistent so the square of "lengthSideA" is named "squareSideA" and the same for the other side?
If you did that then when you tried to write cout << "\n\n Side 1 is " << squareSideA; you would instantly see that you had a strange statement going on.
First
Why are you telling the user about "Side 1" when they gave you information about "Side A"?
Isn't that going to have the user scratching their head about what your program is on about?
Second
Why have you got a variable holding a squared value being output on text that doesn't mention the word "squared"?
Surely that's going to be confusing to the user?
It seems either the text or the variable need to be changed to bring them into agreement with each other.

Four
Combo of meaningful variable names and consistency.
Meaningful.
Why use a variable called "side3" to hold the length of the hypotenuse?
Isn't "hypotenuse" a better name to base the variable name upon?
So maybe "lengthHypotenuse" as a clearer name?
Consistency
As you have written it "side1" and "side2" hold squares of lengths while "side3" holds a simple length.
This would be really confusing for anyone else having to read and maintain your code (including yourself coming back to it in a few month's time).
If you have a naming scheme use it consistently to describe similar things.
Don't mangle fundamentally different things together.
If you find yourself mangling things you have probably discovered a fault in your naming scheme and need to revisit and revise earlier naming choices.
See above for some idea for what could be considered better variable names to avoid this problem.

Five
Just to comment on another piece of advice you were given.
Don't use system("pause")
Read this:
http://www.gidnetwork.com/b-61.html
Was This Post Helpful? 6
  • +
  • -

#9 atm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-September 10

Re: Hypotenuse of a right triangle- C++

Posted 11 September 2010 - 09:43 AM

^I never used system("pause"). What are your opinions on getch() from conio.h library?

I had to use Turbo C v3 in junior college, where we used that. Also clrscr()

*sigh* I remember those days! Terminal is the way to go!!!
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Hypotenuse of a right triangle- C++

Posted 11 September 2010 - 09:48 AM

http://www.dreaminco...topic149914.htm
Was This Post Helpful? 0
  • +
  • -

#11 Huzi94  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 119
  • Joined: 14-November 09

Re: Hypotenuse of a right triangle- C++

Posted 14 September 2010 - 04:12 AM

Hello,
To solve your problem, you can add
cin.get();
to the end of your source code before closing the brackets. And BTW, it's also good to practice to
return 0;
your program.

Cheers...
Was This Post Helpful? 0
  • +
  • -

#12 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Hypotenuse of a right triangle- C++

Posted 16 September 2010 - 06:55 PM

in order to use Huzi94's method you will also need to add a cin.ignore() either after each cin statement, or cin.ignore(x) (where x is the number of cin statements) to hold the window open.

Alternatively, run the program from a command line =)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1