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:
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <windows.h>
#include <math.h>


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
Answer = 5.83095

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.
ok I have added:
1
2
3
4
5
6
7
8
9
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

Please enter a valid number.

But I have no clue how to make it recognize if its a number or not also I would like it to know if its a positive number or not. i was thinking you would have to add something like this:
while (b <= a || // something here that tells whether its a number or not || // and here for a positive number or not)
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
48
49
50
51
52
#include <iostream>
#include <cmath>

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" ;
    }
}

Wow JLBorges thank you so much for this. It would have taken me forever to figure that out haha but i am for all just starting to learn C++. I will study this code and learn from it for future reference. Again thank you.
Topic archived. No new replies allowed.