Im pretty new at c++, i just made this program for Pythagorean theorem. It works fine and everything, but im looking for new skills to shorten this down to as much as possible.
#include <iostream>
#include <cstdlib>
#include <math.h>
usingnamespace std;
int main()
{
cout << "Production of Jack. \n ";//
system("PAUSE");
cout << "This program will find the hypotenuse of a right triangle, \n when given the A value and B value of the triangle. \n";
system("PAUSE");
beginning:
longdouble a;
cout << "Enter A value:";
cin >> a;
longdouble b;
cout << "Enter B value:";
cin >> b;
longdouble c;
c = sqrt ((a*a) + (b*b));
cout << "The hypotenuse is:";
cout << c << endl;
char r;
cout << "do this again? (Y/N)";
cin >> r;
if (r == 'y') // if user enters yes
goto beginning; // redo process
if (r == 'n') // If user enters no
{ cout << "See ya later!\n"; //display farewell
}
system("PAUSE");
}
Okay, this is a bit different from my usual approach to explaining things, but here is an example of your code, shortened in a few basic ways, and commented. :)
#include <iostream>
#include <cstdlib>
#include <cmath>
usingnamespace std; //In the future, you probably shouldn't use this.
int main()
{
cout << "Production of Jack and Flamine Goh.\n ";
cout << "This program will find the hypotenuse of a right triangle,\n ...when given the A value and B value of the triangle. Press Enter to continue. \n";
cin.ignore(); //Similar to system("pause"), except better. ;)
char choice = 'y';
longdouble a, b; //Merged the declarations into one line.
do //Removed the goto.
{
cout << "Enter A value:";
cin >> a;
cout << "Enter B value:";
cin >> b;
cout << "The hypotenuse is:"; //Hypotenuse output/calculation.
cout << sqrt ((a*a) + (b*b)) << endl; //No more long double c.
cout << "Do this again? (Y/N)\n"; //Repeat?
cin >> choice;
} while (choice == 'y' || choice == 'Y'); //Repeats the code after "do" while choice is equal to either 'y' or 'Y'.
cout << "See ya later!\n";
cin.sync(); //After using cin, use this to make the next line work.
cin.ignore(); //Pause.
return 0;
}
I hope this little example helped. Note that I'm writing this half-awake and I didn't test it, so I might have slipped up somewhere.
Basically, what I did for the purposes of shortening your code was:
*Stripped any completely unneeded whitespace.
*Condensed a few of your variable declarations into one line.
*Removed one variable that you didn't need.
All without any major changes to how your program works. :)