-
Pythagorean Programming
Hello, Everyone
Tonight, I was writing a pythagorean program which ask for the two sides and gives the hypothenuse of three different triangles. Here is the program:
Code:
#include<iostream>
using namespace std;
#include<iomanip>
using std::setw;
using std::setprecision;
using std::setiosflags;
#include<cmath>
double hypotenuse(double, double);
int main()
{
double side1, side2, theHypotenuse;
int first = 1;
cout << "Enter the lengths of the two sides: ";
for( int i = 1; i <= 3; i++)
{
cin >> side1 >> side2;
if( first )
{
cout << setw( 5 ) << "Triangle" << setw( 12 ) << "Side 1"
<< setw( 14 ) << "Side 2" << setw( 20 ) << "Hypotenuse"
<< "\n";
first = 0; // prevents this from printing again
}
cout << setiosflags( ios::fixed | ios::showpoint ) << setw( 3 )
<< i << setw( 15 ) << setprecision( 1 ) << side1 << setw( 15 )
<< setprecision( 1 ) << side2 << setw( 15 ) << setprecision ( 1 )
<< hypotenuse << "\n";
}
theHypotenuse = hypotenuse(side1, side2); // The function call
return 0;
}
double hypotenuse(double side1, double side2)
{
double hypot, A;
A = side1 * side1 + side2 * side2;
hypot = sqrt( A );
return(hypot);
}
The program compiles, however, I get the following result:
Code:
<pegasus> g++ hypotenuse.cpp
<pegasus> a.out
Enter the lengths of the two sides: 3.0 4.0 5.0 12.0 8.0 15.0
Triangle Side 1 Side 2 Hypotenuse
1 3.0 4.0 1
2 5.0 12.0 1
3 8.0 15.0 1
<pegasus>
Unfortunately, the answers for the hypotenuse I get are 1.
How can I input into the program to give me the real answer for the hypotenuse as the following:
Code:
Triangle Side 1 Side 2 Hypotenuse
1 3.0 4.0 5.0
2 5.0 12.0 13.0
3 8.0 15.0 17.0
Please someone help me to figure out how to put the real answer as part of the hypotenuse table.
Thank you in advance for all your help.!
-
You were for some reason using the name of the function as output and calling it afterward. That doesn't work. I should also mention that you don't need to define std::setw or any iomanip functions.
It should be like this:
Code:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
double hypotenuse(double, double);
int main()
{
double side1, side2;
int first = 1;
cout << "Enter the lengths of the two sides: ";
for( int i = 1; i <= 3; i++)
{
cin >> side1 >> side2;
if( first )
{
cout << setw( 5 ) << "Triangle" << setw( 12 ) << "Side 1"
<< setw( 14 ) << "Side 2" << setw( 20 ) << "Hypotenuse"
<< "\n";
first = 0; // prevents this from printing again
}
cout << setiosflags( ios::fixed | ios::showpoint ) << setw( 3 )
<< i << setw( 15 ) << setprecision( 1 ) << side1 << setw( 15 )
<< setprecision( 1 ) << side2 << setw( 15 ) << setprecision ( 1 )
<< hypotenuse(side1, side2) << "\n";
}
return 0;
}
double hypotenuse(double side1, double side2)
{
double hypot, A;
A = side1 * side1 + side2 * side2;
hypot = sqrt( A );
return(hypot);
}
Output:
Code:
Enter the lengths of the two sides: 3.0 4.0 5.0 12.0
Triangle Side 1 Side 2 Hypotenuse
1 3.0 4.0 5.0
2 5.0 12.0 13.0
-
SlyMaelstrom:
I got your message. Once again, I could solve the problem with inputting hypotenuse answer as a table. Thank you for your tips.
I did not know you don't need to define std::setw or any iomanip functions in these types of program.
Sincerely, thank you for your help and your precious time. :) :) !
-
when doing
"using namespace std;" there is no need for
"using std::<blah>" since its already taken care of