trig calculator

This is a discussion on trig calculator within the C++ Programming forums, part of the General Programming Boards category; just finished my first program which is actually usefull to me. It just figures out right angle triangles Code: #include ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    29

    trig calculator

    just finished my first program which is actually usefull to me.
    It just figures out right angle triangles

    Code:
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <fstream>
    using namespace std;
    void welcome();
    float decision(string);
    float sides(float &, float &, float &, float &, float &);
    int main(int argc, char *argv[])
    {
    welcome();
    
      return 0;
    }
    void welcome(){
    int menu;
    float valuea;
    float valueb;
    float valuec;
    float valueao;
    float valuebo;
    float valueco;
    cout << "Trig Calculator" << endl << endl;
    cout << "Select problem type\n1. Right angle triangle" << endl;
    cin >> menu;
    switch(menu){
    case 1:
    
     cout << "If you do not know the value use 0.\n\n";
     valuea = decision("Do you know side A?\n");
     valueb = decision("Do you know side B?\n");
     valuec = decision("Do you know side C?\n");
     valueao = decision("Do you know angle A?\n");
     valuebo = decision("Do you know angle B?\n");
     sides(valuea, valueb, valuec, valueao, valuebo);
     cout << "\n\nTriangle calculated:\n\n";
     cout << "Side A: " << valuea << endl;
     cout << "Side B: " << valueb << endl;
     cout << "Side C: " << valuec << endl;
     cout << "Angle A: " << valueao << endl;
     cout << "Angle B: " << valuebo << endl << endl;
     welcome();
     }
    
    }
    float decision(string question){
    float value;
    float radian = 3.1415/180;
    cout << question;
    cin >> value;
    return value;
    }
    float sides(float &a, float &b, float &c, float &ao, float &bo){
    float radian = 3.1415/180;
    if(a !=0 && b!=0 && c==0){
    c = sqrt(a * a + b * b);
    }
    if(a !=0 && c!=0 && b==0){
    b = sqrt(c * c - a * a);
    }
    if(a ==0 && c!=0 && b!=0){
    a = sqrt(c * c - b * b);
    }
    //TAN with side A
    if(a!= 0 && b==0 && c==0){
    if(ao !=0){
    b = a/tan(ao*radian);
    if(bo == 0){
    bo = 90-ao;
    }
    }
    if(bo !=0){
    b = a*tan(bo*radian);
    if(ao == 0){
    ao = 90-bo;
    }
    }
    c = sqrt((a*a)+(b*b));
    }
    //tan with sideB
    if(a == 0 && b!=0 && c==0){
    if(ao !=0){
    a = b*tan(bo*radian);
    if(bo == 0){
    bo = 90-ao;
    }
    }
    if(bo !=0){
    a = b/tan(bo*radian);
    if(ao == 0){
    ao = 90-bo;
    }
    }
    c = sqrt((a*a)+(b*b));
    }
    //sin with sideC
    if(a == 0 && b==0 && c!=0){
    if(ao !=0){
    a = c*sin(ao*radian);
    if(bo == 0){
    bo = 90-ao;
    }
    }
    if(bo !=0){
    a = c*cos(bo*radian);
    if(ao == 0){
    ao = 90-bo;
    }
    }
    b = sqrt((c*c)+(a*a));
    }
    if(ao == 0 && bo ==0){
    ao = atan(a/b)*180/3.141592654;
    bo = 90 - ao;
    }
    }
    keep in mind I started using c++ only a few days ago. Any sugestions?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Well, this is your main() function:

    Code:
    int main(int argc, char *argv[])
    {
    welcome();
    
      return 0;
    }
    which should be the main hub of activity, but it looks like a ghost town. I would get rid of your welcome() function and move all that code into main().

    It looks like your switch statement has one case--that means it's just an if statement.

    The recursive welcome() function call in your switch statement is not a very elegant construct. You should consider using a while loop that terminates when you enter a certain number.

    If you can get more than 10 lines to compile when you are first starting--much less produce correct output-- you are way ahead of the game.
    Last edited by 7stud; 02-17-2005 at 11:12 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    You could try learning some indentation techniques while you're at it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Salem
    You could try learning some indentation techniques while you're at it.
    I second that notion.

    edit: I indented your code for you, and all I have to say is, find a better way... use less variables, clean up your conditional statements, and get rid of the case in void welcome() (which should just be the code in main():
    Code:
    void welcome()
    {
        int menu;
        float valuea;
        float valueb;
        float valuec;
        float valueao;
        float valuebo;
        float valueco;
        
        for(;;)
            cout << "Trig Calculator" << endl << endl;
            cout << "Select problem type\n1. Right angle triangle" << endl;
            cin >> menu;
            
            if(menu==1)
                break;
            
            cout << "If you do not know the value use 0.\n\n";
            valuea = decision("Do you know side A?\n");
            valueb = decision("Do you know side B?\n");
            valuec = decision("Do you know side C?\n");
            valueao = decision("Do you know angle A?\n");
            valuebo = decision("Do you know angle B?\n");
            
            sides(valuea, valueb, valuec, valueao, valuebo);
            
            cout << "\n\nTriangle calculated:\n\n";
            cout << "Side A: " << valuea << endl;
            cout << "Side B: " << valueb << endl;
            cout << "Side C: " << valuec << endl;
            cout << "Angle A: " << valueao << endl;
            cout << "Angle B: " << valuebo << endl << endl;
        }
    }
    and the rest of your indented code (your original, untouched code)
    Code:
    #include <stdio.h>
    #include <string>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <fstream>
    
    
    using namespace std;
    
    void welcome();
    float decision(string);
    float sides(float &, float &, float &, float &, float &);
    
    int main(int argc, char *argv[])
    {
            welcome();
            return 0;
    }
    
    void welcome()
    {
        int menu;
        float valuea;
        float valueb;
        float valuec;
        float valueao;
        float valuebo;
        float valueco;
        
        cout << "Trig Calculator" << endl << endl;
        cout << "Select problem type\n1. Right angle triangle" << endl;
        cin >> menu;
        
        switch(menu)
        {
            case 1:
                 cout << "If you do not know the value use 0.\n\n";
                 valuea = decision("Do you know side A?\n");
                 valueb = decision("Do you know side B?\n");
                 valuec = decision("Do you know side C?\n");
                 valueao = decision("Do you know angle A?\n");
                 valuebo = decision("Do you know angle B?\n");
                 sides(valuea, valueb, valuec, valueao, valuebo);
                 cout << "\n\nTriangle calculated:\n\n";
                 cout << "Side A: " << valuea << endl;
                 cout << "Side B: " << valueb << endl;
                 cout << "Side C: " << valuec << endl;
                 cout << "Angle A: " << valueao << endl;
                 cout << "Angle B: " << valuebo << endl << endl;
                 welcome();
         }
    
    }
    
    float decision(string question)
    {
        float value;
        float radian = 3.1415/180;
        
        cout << question;
        cin >> value;
        
        return value;
    }
    
    float sides(float &a, float &b, float &c, float &ao, float &bo)
    {
        float radian = 3.1415/180;
        if(a !=0 && b!=0 && c==0)
        {
            c = sqrt(a * a + b * b);
        }
        if(a !=0 && c!=0 && b==0)
        {
            b = sqrt(c * c - a * a);
        }
        if(a ==0 && c!=0 && b!=0)
        {
        a = sqrt(c * c - b * b);
        }
        //TAN with side A
        if(a!= 0 && b==0 && c==0)
        {
            if(ao !=0)
            {
                b = a/tan(ao*radian);
                if(bo == 0)
                {
                    bo = 90-ao;
                }
            }
            if(bo !=0)
            {
                b = a*tan(bo*radian);
                    if(ao == 0)
                    {
                        ao = 90-bo;
                    }
            }
            c = sqrt((a*a)+(b*b));
        }
        //tan with sideB
        if(a == 0 && b!=0 && c==0)
        {
            if(ao !=0)
            {
                a = b*tan(bo*radian);
                if(bo == 0)
                {
                    bo = 90-ao;
                }
            }
            if(bo !=0)
            {
                a = b/tan(bo*radian);
                if(ao == 0)
                {
                    ao = 90-bo;
                }
            }
            c = sqrt((a*a)+(b*b));
        }
        //sin with sideC
        if(a == 0 && b==0 && c!=0)
        {
            if(ao !=0)
            {
                a = c*sin(ao*radian);
                if(bo == 0)
                {
                    bo = 90-ao;
                }
            }
            if(bo !=0)
            {
                a = c*cos(bo*radian);
                if(ao == 0)
                {
                    ao = 90-bo;
                }
            }
            b = sqrt((c*c)+(a*a));
        }
        if(ao == 0 && bo ==0)
        {
            ao = atan(a/b)*180/3.141592654;
            bo = 90 - ao;
        }
    }
    I just noticed this:
    Code:
    float radian = 3.1415/180;
    Code:
    ao = atan(a/b)*180/3.141592654;
    please, for the sake of sanity, consider using a constant here, a simple line of code would save you a world of confusion:
    Code:
    const double pi=31.141592654;
    Last edited by major_small; 02-18-2005 at 05:08 AM. Reason: fixing OP's code, fixing my "Fixed" verision of the OP's code...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Pi ~ 31, eh?
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    30
    lol, i think he might've meant:

    const double pi=3.141592654;

    but if 31 is what he wanted.....
    ADRUMSOLO4U

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah, I actually meant 31,415,926.54
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Hmm... I see a problem if Side C == 0, and either Angle A or B == 0. Then it will never calculate Angle A or B!..
    Or am I totally wrong?
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    29
    yea, but that's an unlikely situation. I wrote it mostly for when I'm doing physics stuff on the computer and don't want to use windows calc

  10. #10
    the Wizard
    Join Date
    Aug 2004
    Posts
    109
    Yes, it's very unlikely, but I just fought of it.
    If you had to publish your work some day, and a person switched between the letters, so that the 90degree angle would be B instead of C. Then you would have a problem. But if it's for home use only, then it would do with the 3 if-sentences alone
    -//Marc Poulsen -//MipZhaP

    He sat down, he programmed, he got an error...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Calculator - Critique
    By The Brain in forum Windows Programming
    Replies: 1
    Last Post: 02-25-2006, 03:39 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  4. Java Calculator
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-12-2002, 07:39 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

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