### More Efficient

I am new to C++, and for a beginner project I figured I could write a program that does the Pythagorean Theorem. It's work's like a charm ( i think haha) also it's accurate enough, but I wanted to know if I wrote more than i had to. I was wondering what I could do to shorten it and make it more efficient or did I miraculously write my first program pretty good? haha anywho here's the Code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include #include using namespace std; void findc() { double answer; int a,b,c; cout << "\nEnter A: "; cin >> a; cout << "\nEnter B: "; cin >> b; c = (b*b) + (a*a); answer = sqrt(c); cout << "\nAnswer : " << answer; cin.ignore (); } void findaorb() { double answer; int a,b,c; cout << "\nEnter C: "; cin >> b; cout << "\nEnter A or B: "; cin >> a; c = (b*b) - (a*a); answer = sqrt(c); cout << "\nAnswer :" << answer; } int main () { SetConsoleTitle ("Pythagorean Theorem Calculator!"); int input; cout<<"1. To Find C\n"; cout<<"2. To Find A or B\n"; cout<<"3. Exit\n\n"; cout<<"Selection: "; cin>> input; cin.ignore (); switch ( input ) { case 1: findc(); break; case 2: findaorb(); break; case 3: cout<<"Thank You For Using TheFlyingByrd's Pythagorean Theorem Program! :) \n"; break; default: cout<<"Sorry, Try Again.\n"; break; } cin.get(); return 0; }``````
Last edited on
At line 31, c may be negative. I think you should test that before the sqrt().

Perhaps a, b and c should be of type double too, I don't see why not.

Not sure about line 66, just as the program ends, setting the title?

On the plus side, I don't think this is overly long, almost all the code serves a purpose. It's also good to see the use of functions to break the code into manageable chunks. The original question asked about making the code shorter or more efficient, well mostly any changes would be more a matter of style or preference rather than a real improvement, at least in my opinion.
Last edited on
Wow haha I didn't even notice that I had my title to set at the end I wasnt paying attention thank you. (p.s. I've moved it, so now its set from the beginning)

also would changing them from int to double really make a difference? what are the advantages to doing this?

How can c be negative? when finding a or b, c will always be the largest number so when the user inputs for "Enter C:" they will enter a larger number than the one they input for "Enter A or B:"
Last edited on
Why change to double? Well, let's say the user is entering sides A and B.
A = 3
B = 5

So far so good.

But what if the user wants to enter C = 5.83095 ?
Currently they are not able to do so.

How can c be negative? I suppose you could politely ask the users to enter sensible data. But it would be good practice to validate the input to confirm that is the case, and maybe output a message if the values cannot make a triangle.

Broadly speaking, users will sooner or later accidentally or deliberately enter invalid data, maybe through a simple typing error, or lack of concentration. The program should be able to handle that.

Still, perhaps that's a larger topic, including for example handling non-numeric data as well. At this stage you can regard it as something to be aware of for the future.
 ``123456789`` ``````do { cout << "\nPlease enter a valid number"; cout << "\n\nEnter C: "; cin >> b; cout << "\nEnter A or B: "; cin >> a; } while (b <= a);``````

so now if the user enters a number for C but then enters a larger number for A or B it will ask for another entry until its right.

One thing i would like to do is have the same statement pop up if the user inputs any character thats not a number example:
Enter C: 3

Enter A or B: TheFlyingByrd

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````#include #include double get( const char* prompt, double less_than = 1000000 ) { double number ; std::cout << prompt << ": " ; if( std::cin >> number && number > 0 && number < less_than ) return number ; // user has entered either something that is not a number, // or a number which is not in the valid range std::cout << "please enter a positive number less than " << less_than << '\n' ; std::cin.clear() ; // clear error state, if any // http://www.cplusplus.com/reference/ios/ios/clear/ std::cin.ignore( 1000, '\n' ) ; // throw away unwanted input // http://www.cplusplus.com/reference/istream/istream/ignore/ return get( prompt, less_than ) ; // and try again } void findc() { double a = get( "enter A" ), b = get( "enter B" ) ; std::cout << "answer: " << std::sqrt( a*a + b*b ) << '\n' ; } void findaorb() { double c = get( "enter C" ), aorb = get( "enter A or B", c ) ; std::cout << "answer: " << std::sqrt( c*c - aorb*aorb ) << '\n' ; } int main () { std::cout << "1. To Find C\n" "2. To Find A or B\n" "3. Exit\n\n" "Selection: " ; char selection ; std::cin >> selection ; switch(selection) { case '1': findc() ; break ; case '2': findaorb() ; break ; case '3': std::cout << "thank you ... :)\n" ; default: std::cout << "invalid choice\n" ; } }``````