How can I shorten this?

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include <math.h>


using namespace 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:
long double a;
cout << "Enter A value:";
cin >> a;

 long double b;
cout << "Enter B value:";
cin >> b;

 long double 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. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace 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';
    long double 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. :)

-Albatross
Last edited on
Thank you! I was told using "goto" was a bad idea because it can unknowingly create infinite loops.
Topic archived. No new replies allowed.