sponge cake recipe Grandmaster Cheater Supreme
Reputation: 22
Joined: 24 Sep 2007 Posts: 1635
|
Posted: Sun Feb 07, 2010 1:24 am Post subject: [C++] Simple Pythagoras Calculator |
|
|
I didn't make this for any particular reason, just wanted to practice some C++, and try to get a bit better.
I'm pretty sure this all functions perfectly, but I need some criticism.
Is this the best way it could be coded, and is there anything that I should add to improve it?
Keep in mind I'm a (complete) beginner.
Cheers.
Code: | #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void calchyp(float side_a, float side_b);
void calcside(float side_a, float side_b);
int main()
{
int pick;
float side_a, side_b;
cout << "Please enter the length of side one." << endl;
cin >> side_a;
cout << "Please enter the length of side two." << endl;
cin >> side_b;
cout << "To find the hypotenuse, type '1' without the quotes and press enter." << endl;
cout << "To find one of the sides, type '2'." << endl;
cin >> pick;
if (pick == 1) calchyp (side_a, side_b);
if (pick == 2) calcside (side_a, side_b);
system("pause");
return 0;
}
void calchyp(float side_a, float side_b)
{
float hyp;
int rounding;
cout << "Please enter the number of decimals you'd like it to be rounded to." << endl;
cin >> rounding;
hyp = sqrt(pow(side_a, 2) + pow(side_b, 2));
cout << setprecision (rounding + 1) << "Hypotenuse = : " << hyp << endl;
}
void calcside(float side_a, float side_b)
{
float side;
int rounding;
cout << "Please enter the number of decimals you'd like it to be rounded to." << endl;
cin >> rounding;
side = sqrt(pow(side_b, 2) - pow(side_a, 2));
cout << setprecision (rounding + 1) << "Side = : " << side << endl;
} |
|
|
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Sun Feb 07, 2010 7:46 am Post subject: |
|
|
Since you do the follwing in bóth calchyp and calcside, you might as well put it in your main function, right after you've asked the user for the sides. Then you can pass it as an extra argument to calchyp and calcside.
Code: | cout << "Please enter the number of decimals you'd like it to be rounded to." << endl;
cin >> rounding;
|
And system("pause"); is a bad practice. Instead you can use:
cin.sync();
cin.ignore();
|
|