C++ Pythag Calculator

This is a discussion on C++ Pythag Calculator within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <stdio.h> #include <math.h> using namespace std; int main() { int A, B, C, A1, B1, C1, ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    31

    C++ Pythag Calculator

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    
    
    using namespace std;
    
    int main()
    {
    int A, B, C, A1, B1, C1, A2, B2, C2, A3, B3, C3, A4, B4, C4 ;
    
        cout << "Welcome to Maths Cheat. A program developed by Kej. \nI hope it helps! \n";
        cout << "Pythag mode has been initialized. \nInput the variables in the form of A B C, with either B or C as 0 dependent\n on whether the hypotenuse is known. \n";
        cout << "Now enter your variables in the form A B C Then press enter.";
        cin >> A >> B >> C;
        cout << "A = " << A <<"\n";
        cout << "B = " << B <<"\n";
        cout << "C = " << C <<"\n";
    
        if (C == 0){
            C1 = pow (A,2);
            C2 = pow (B,2);
            C3 = C1 + C2;
            C4 = sqrt (C3);
         cout << "Calculation in progess \nC = " << C4;
    
        }
    
    
    
    }
    Run this program why does C4 = 4 if you input 2 4 0, and not 5 as it should be?

    Only just learning about math functions.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The answer for 2 4 0 should be ~4.47, but since you (implicitly) cast that result to int, the value is truncated and 4 remains.

    In this particular case, 4 happens to be closer to the real value.

    The worrying thing is that you are using integers to store the results of calculations that result in real numbers.
    Last edited by anon; 09-19-2009 at 05:07 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    I see. So how can I get the program to output it to 2 decimal places?

    BTW it did orignally execute under one variable, I changed it to break the calculation down stage by stage In an attempt to debug it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is no such thing as an int with 2 decimal places. A data type of int is inappropriate for this problem.

    As far as your other printing problem goes, you should examine "setprecision".

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <math.h>
    
    
    using namespace std;
    
    int main()
    {
    long double A, B, C, X, Y, Z;
    
        cout << "Welcome to Maths Cheat. A program developed by Kej. \nI hope it helps! \n";
        cout << "Pythag mode has been initialized. \nInput the variables in the form of A B C, with either B or C as 0 dependent\n on whether the hypotenuse is known. \n";
        cout << "Now enter your variables in the form A B C Then press enter.";
        cin >> A >> B >> C;
        cout << "A = " << A <<"\n";
        cout << "B = " << B <<"\n";
        cout << "C = " << C <<"\n";
    
        if (C == 0){
            Z = pow (10 ^ 2) + pow (10 ^ 2)
                sqrt (Z)
                cout << "Calculation in progess \nC = " << Z;
    
        }
    
    
    
    }
    That's the new code, now getting this error message. Can someone explain floating point numbers to me, or give me some tutorial links?

    Code:
    _CRTIMP	double __cdecl pow (double, double);

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So how many arguments are you passing to pow? (Also, ^ means xor.)

    This code makes even less sense. I'd also like to point out, that a rather straightforward way to square a value is x * x.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by anon View Post
    So how many arguments are you passing to pow? (Also, ^ means xor.)

    This code makes even less sense. I'd also like to point out, that a rather straightforward way to square a value is x * x.
    as in
    Code:
    Result = sqrt((VectorX * VectorX) + (VectorY * VectorY));
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Quote Originally Posted by abachler View Post
    as in
    Code:
    Result = sqrt((VectorX * VectorX) + (VectorY * VectorY));
    This is why I hate coding sometimes, I make it more complicated than it is, thanks for both of you for the help.

    Problem solved, it works.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Code:
                else {
                        cout << "Trig mode has been initialized. Do you need to find an angle : yes (1), No (2). Enter you selection now!\n";
                            cin >> menu;
    
                                if (trigmenu == 1){
                                    cout << "You choose to find a missing angle.";
    
                                }
                                else {
                                    cout << "You choose to find a missing side.\n";
                                    cout << "Enter your variables in the form of Adj, Opp, Hyp, Angle.\nLeaving variables blank as appropriate.";
                                    cin >> Adj >> Opp >> Hyp >> Angle;
                                                    cout << "Adj = " << Adj <<"\n";
                                                    cout << "Opp = " << Opp <<"\n";
                                                    cout << "Hyp = " << Hyp <<"\n";
                                                    cout << "Angle = " << Angle <<"\n";
    
                                                        if (Hyp = 0){
                                                                cout << "test";
                                                        }
    
                                }
            }
    Pythag section now works, I'm now developing the trig section, run into a block, this part of the program runs as far as displaying what the user entered for each variable it fails to print test at the end, any ideas.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Note that Hyp = 0 is always 0, hence always false, hence the true part of the if will never execute. (Now Hyp == 0 could be true and could be false.)

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Slaps head against table, why didn't I see that.

    EDIT

    sorry for all these noobish questions.

    Code:
                                else {
                                    cout << "You choose to find a missing side.\n";
                                    cout << "Enter your variables in the form of Adj, Opp, Hyp, Angle.\nLeaving variables blank as appropriate.";
                                    cin >> Adj >> Opp >> Hyp >> Angle;
                                                    cout << "Adj = " << Adj <<"\n";
                                                    cout << "Opp = " << Opp <<"\n";
                                                    cout << "Hyp = " << Hyp <<"\n";
                                                    cout << "Angle = " << Angle <<"\n";
    
                                                        if (Hyp == 0){
                                                            Hyp1 = sin(35);
                                                                cout << "test" << Hyp1;
                                                        }
    
                                }
    Why does sin 35 print as a completely wrong value?

    My calculator outputs it as 0.5735764364, but the program outputs -0.428183. Noting my calculator is programmed to Math10
    Last edited by headshot119; 09-20-2009 at 03:45 PM.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    The C++ trigonometric functions work in radians rather than degrees.

    Radians

    I guess this is the issue you are experiencing.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    As mentioned previously, you'll have to convert to radians from degrees if you want the sin of 35 degrees. To convert from degrees to radians, multiply by this fraction: pi / 180. IIRC, C++ doesn't have a standard constant for the value of Pi, so you'll have to declare your own constant for Pi.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Thanks guys much appreciated, while that wasn't working I moved onto the quadratic equation and did it with no problems, I must have learnt something :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help modifying my calculator!!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 03-25-2008, 01:03 PM
  2. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 04:39 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 10:48 AM
  5. Replies: 2
    Last Post: 05-10-2002, 05:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21