Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (4)

Powered by Vanilla. Made with Bootstrap.
[C++] Pythagorean calculator
  • Sh3llc0d3
    Posts: 1,910
    I'm into quite technical maths and wanted to use what i'd learned to make this. Took a bit of tinkering but i'm pleased with it.

    //Semtex-Primed's Pythagorean theorum calulator v1.00

    #include <iostream>
    #include <cmath>

    int main()
    {
    double x, y, hyp; //variables

    std::cout << \"Semtex-Primed's Pythagorean theorum calulator v1.00\n\" << \"-- You will need the length of the longest side and the shortest side both leading away from the right-angle.\";

    std::cout << \"\n\n\"; //blank line output

    Again:
    //input of longest side
    std::cout << \"Enter length of the longest side leading away from the 90-degree angle: \";
    std::cin >> x;

    if(x <= 0)
    {
    std::cout << \"Sorry, the inputted number for the longest side cannot be correct, enter a number above 0\" << \"\n\";
    goto Again;
    }

    Again2:
    //input of shorter side
    std::cout << \"Enter length of the shorter side leading away from the 90-degree angle: \";
    std::cin >> y;

    if(y <= 0)
    {
    std::cout << \"Sorry, the inputted number for the shortest side cannot be correct, enter a number above 0\" << \"\n\";
    goto Again2;
    }
    //calculation area
    hyp = sqrt(x*x + y*y);

    //answer being outputted.
    std::cout << \"The length of your Hypotenuse is: \" << hyp;

    return 0;
    }


    Quite pleased the goto's work, thanks peeps
  • Nice basic program. Just dropping my 2 cents : Try avoiding goto in complex programs and work without it, it usually fucks up when something goes wrong and ends up in a crash or infinite loop
  • Sh3llc0d3
    Posts: 1,910
    Since a while ago I have stopped using goto's in most of my programs. I however did not see this as a complex enough project for problems to arise.
  • Xin
    Posts: 3,251
    Nice :) , dont really code in python but it looks good
    Xin
  • It isnt python, it is c++
  • Xin
    Posts: 3,251
    Oh yeah my bad, the py in pythagorian made me think :L
    Xin
  • undead
    Posts: 822
    said:


    Nice :) , dont really code in python but it looks good



    how would it be python since it's in c/c++ section? :P
  • Goto's are bad practice.

    On HF, they led me to 2/3 pages of flaming. I suggest you switch it with a While loop, and returning functions.


    // Testing Void main.cpp : Defines the entry point for the console application.
    //
    //Semtex-Primed's Pythagorean theorum calulator v1.00
    #include <iostream>
    #include <cmath>

    double firstside(double x); //telling the compiler to look elsewhere in the code for the functions
    double secondside(double y, double x);
    double calculation(double x, double y);

    int main(void)
    {
    static double x, y, hyp; //variables. I forgot if I needed static or not o.

    std::cout << \"Semtex-Primed's Pythagorean theorum calulator v1.00, with a coupla changes from CtrlAltxGhostx\n\n\" << \"-- You will need the length of the longest side and the shortest side both leading away from the right-angle.\n\n\";

    while(1) //Beginning of While loop
    { // While loop block inside curly braces
    x = firstside(x); //Using the function..
    y = secondside(y, x);//\"
    hyp = calculation(x, y);//\"

    //answer being outputted.
    std::cout << \"The length of your Hypotenuse is: \" << hyp;
    //pause
    std::cout << \"\nPress <enter> to continue...\" << std:: endl;
    std::cin.sync();
    std::cin.get();
    //end of pause
    }//end of While loop
    }

    double firstside(double x)
    {
    //input of longest side
    std::cout << \"Enter length of the longest side leading away from the 90-degree angle: \";
    std::cin >> x;
    if(x <= 0) //error checking
    {
    std::cout << \"Sorry, the inputted number for the longest side cannot be correct, enter a number above 0\" << \"\n\";
    firstside(x); //recall of the function - replaces goto
    }
    return(x); //return the value

    }

    double secondside(double y, double x)
    {
    //input of shorter side
    std::cout << \"Enter length of the shorter side leading away from the 90-degree angle: \";
    std::cin >> y;
    if(y <= 0 || y >= x) //error checking. It also checks to see if the value is larger than x.
    {
    std::cout << \"Sorry, the inputted number for the shorter side cannot be correct, enter a number above 0\" << \"\n\";
    secondside(y, x);//recall of the function - replaces goto
    }
    return(y);//return the value
    }

    double calculation(double x, double y){return(sqrt(x*x + y*y));} //one line calculatory functions are nice :)



    I personally have no problem with goto, but it IS better to use While and functions.

    Look at my code - It is good to practice function and loop usage.

    Apart from that, good for a beginner's program :)

    Off Topic: HQ first post ftw?
  • Sh3llc0d3
    Posts: 1,910
    a) this isn't HF.
    b) It was my 2nd program in C++, I realised shortly after making the program GOTO's are not great, funnily enough, in newer program's i made in C++ i tried to take them out and use while alot more.

    rant
    I refuse to post at all in the C++ section on that particular site for a number of reasons. Mostly because the majority of people on HF who know C++ are elitest arseholes about it and flame anyone (without going into names). Ganeral statement, however I think your post supports it.
    /rant

    Thanks for the advice and it was a very HQ 1st post lol. Sorry for the frosty reception I just have a real problem with HF, well more the coding section lol :)
  • Orgy
    Posts: 40
    said:


    Goto's are bad practice.

    On HF, they led me to 2/3 pages of flaming. I suggest you switch it with a While loop, and returning functions.


    // Testing Void main.cpp : Defines the entry point for the console application.
    //
    //Semtex-Primed's Pythagorean theorum calulator v1.00
    #include <iostream>
    #include <cmath>

    double firstside(double x); //telling the compiler to look elsewhere in the code for the functions
    double secondside(double y, double x);
    double calculation(double x, double y);

    int main(void)
    {
    static double x, y, hyp; //variables. I forgot if I needed static or not o.

    std::cout << \"Semtex-Primed's Pythagorean theorum calulator v1.00, with a coupla changes from CtrlAltxGhostx\n\n\" << \"-- You will need the length of the longest side and the shortest side both leading away from the right-angle.\n\n\";

    while(1) //Beginning of While loop
    { // While loop block inside curly braces
    x = firstside(x); //Using the function..
    y = secondside(y, x);//\"
    hyp = calculation(x, y);//\"

    //answer being outputted.
    std::cout << \"The length of your Hypotenuse is: \" << hyp;
    //pause
    std::cout << \"\nPress <enter> to continue...\" << std:: endl;
    std::cin.sync();
    std::cin.get();
    //end of pause
    }//end of While loop
    }

    double firstside(double x)
    {
    //input of longest side
    std::cout << \"Enter length of the longest side leading away from the 90-degree angle: \";
    std::cin >> x;
    if(x <= 0) //error checking
    {
    std::cout << \"Sorry, the inputted number for the longest side cannot be correct, enter a number above 0\" << \"\n\";
    firstside(x); //recall of the function - replaces goto
    }
    return(x); //return the value

    }

    double secondside(double y, double x)
    {
    //input of shorter side
    std::cout << \"Enter length of the shorter side leading away from the 90-degree angle: \";
    std::cin >> y;
    if(y <= 0 || y >= x) //error checking. It also checks to see if the value is larger than x.
    {
    std::cout << \"Sorry, the inputted number for the shorter side cannot be correct, enter a number above 0\" << \"\n\";
    secondside(y, x);//recall of the function - replaces goto
    }
    return(y);//return the value
    }

    double calculation(double x, double y){return(sqrt(x*x + y*y));} //one line calculatory functions are nice :)



    I personally have no problem with goto, but it IS better to use While and functions.

    Look at my code - It is good to practice function and loop usage.

    Apart from that, good for a beginner's program :)

    Off Topic: HQ first post ftw?


    Keep in mind however, that HF is full of skids that don't actually know what they're talking about. Someone tells them something such as "gotos are bad practice" and they just accept it. They don't question it or ask why. They just assume the first person to tell them something is correct. They're like babies, taking in the first bit of knowledge and not accepting anything else.
  • Sh3llc0d3
    Posts: 1,910
    Well said Orgy, I understand why people do get pissed off with noobs but if they aren't prepared to teach them the correct way I don't see the point in commenting (apart from the cement their ego's)... just my opinion though. I personally found a great user elsewhere who would explain everything if I had a problem. I believe Vorfin summed up the coding sub-forums there very well, wish I could find the quote lol.
  • Oh I agree. I think goto's are fine when used appropriately.

    The problem ensues when people do not use them correctly(Don't use If clause for error-checking, par example).

    An example of HF:
    "sbrg" is an asshole, who loves flaming my threads. A guy was asking for help with his code, and I rewrote it with comments to show where he needed to change it.

    I end up with 3 pages of flaming, instigated by him because of one comment(out of 20+ helping ones) at the top suggesting "void main()" instead of "int main()", because it isn't valid C++.

    However, it compiles fine in VC++. I didn't get any thanks for spending half an hour helping him, and just get 3/4 people flaming me.

    /semi - rant.

    Anyway, if people post sources (for help) like this, expect me to help out, particularly in C++ and AutoIT.

    P.S Careful with the wording. I was helping.
  • Sh3llc0d3
    Posts: 1,910
    [spoiler=LOL] Actually I thought you might bring that particular person up. I actually read the thread your talking about earlier I think the argument being whether it meets the 'standard' or not and not whether it compiles. He's a good coder, his knowledge shows through sometimes, however his methods and general arrogance is beyond belief and clouds the issue. He likes being the person who spots an error and rips the person to bits, if he thinks your prone to making school-boy errors he'll follow you about. I've heard it said a few times he's done the same, thread stalking that is.[/spoiler]


    I'm all for people helping and I'll be the last person to turn down advice/help in C++ (as anyone who's learned it will surely appreciate). It's a big learning curve, especially with little to no background in coding. I'll be learning alot more detail on c++/c at University next year so hopefully i'll be able to get shot of vb.net full stop lol (once i've finished my RAT that is).
  • said:


    [spoiler=LOL] Actually I thought you might bring that particular person up. I actually read the thread your talking about earlier I think the argument being whether it meets the 'standard' or not and not whether it compiles. He's a good coder, his knowledge shows through sometimes, however his methods and general arrogance is beyond belief and clouds the issue. He likes being the person who spots an error and rips the person to bits, if he thinks your prone to making school-boy errors he'll follow you about. I've heard it said a few times he's done the same, thread stalking that is.[/spoiler]


    I'm all for people helping and I'll be the last person to turn down advice/help in C++ (as anyone who's learned it will surely appreciate). It's a big learning curve, especially with little to no background in coding. I'll be learning alot more detail on c++/c at University next year so hopefully i'll be able to get shot of vb.net full stop lol (once i've finished my RAT that is).



    I understand where he is coming from. However, my confusion generally ensues from VC++ not enforcing the standard.

    VC++ doesn't care about int main() etc., so I have Code::Blocks now, and NetBeans if I can be bothered to set it up.

    However, the whole thread could have been avoided if he had just gone "Actually, you should be using 'int main(void)'" or something along those lines.

    I learnt a bunch of AutoIT, and then a bit of everything really. Enough VB .NET to get by, the same with C#, a little Python, but none fluently. My dad is a software dev, who runs a team, of 12 for a SatNav company, and he said go learn C++. So I am.

    If you ever need any help, I'll help you out, without calling you a "fucking idiot", unlike several people on HF.
  • sangf
    Posts: 203


    However, my confusion generally ensues from VC++ not enforcing the standard.

    VC++ doesn't care about int main() etc., so I have Code::Blocks now, and NetBeans if I can be bothered to set it up.

    However, the whole thread could have been avoided if he had just gone "Actually, you should be using 'int main(void)'" or something along those lines.



    here's why: http://connect.microsoft.com/VisualStud ... -a-warning (although i don't understand their logic - other compilers manage fine). anyway as you'll see from the last message, it does report a warning in strict mode. you'll find most compilers have some non standard quirks and undefined behavior - not everything is standards validated, the programmer takes responsibility. imo, the msvc compiler is a good choice on windows. what compiler does code::blocks use as default out of curiosity? also, i believe int main(void) is a c99 specified definition whereas c++ prefers simple int main().
  • Sh3llc0d3
    Posts: 1,910
    code::blocks uses Mingw compiler if you use the windows installer with built in compiler. You can customise to use a different compiler though.