Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] Simple Pythagoras Calculator

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
sponge cake recipe
Grandmaster Cheater Supreme
Reputation: 22

Joined: 24 Sep 2007
Posts: 1635

PostPosted: Sun Feb 07, 2010 1:24 am    Post subject: [C++] Simple Pythagoras Calculator Reply with quote

I didn't make this for any particular reason, just wanted to practice some C++, and try to get a bit better.
I'm pretty sure this all functions perfectly, but I need some criticism.
Is this the best way it could be coded, and is there anything that I should add to improve it?

Keep in mind I'm a (complete) beginner.
Cheers.

Code:
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

void calchyp(float side_a, float side_b);
void calcside(float side_a, float side_b);

int main()
{
    int pick;
    float side_a, side_b;

    cout << "Please enter the length of side one." << endl;
    cin >> side_a;

    cout << "Please enter the length of side two." << endl;
    cin >> side_b;

    cout << "To find the hypotenuse, type '1' without the quotes and press enter." << endl;
    cout << "To find one of the sides, type '2'." << endl;
    cin >> pick;

    if (pick == 1) calchyp (side_a, side_b);
    if (pick == 2) calcside (side_a, side_b);

    system("pause");
    return 0;
}

void calchyp(float side_a, float side_b)
{
    float hyp;
    int rounding;

    cout << "Please enter the number of decimals you'd like it to be rounded to." << endl;
    cin >> rounding;

    hyp = sqrt(pow(side_a, 2) + pow(side_b, 2));
    cout << setprecision (rounding + 1) << "Hypotenuse = : " << hyp << endl;
}

void calcside(float side_a, float side_b)
{
    float side;
    int rounding;

    cout << "Please enter the number of decimals you'd like it to be rounded to." << endl;
    cin >> rounding;

    side = sqrt(pow(side_b, 2) - pow(side_a, 2));
    cout << setprecision (rounding + 1) << "Side = : " << side << endl;
}
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Sun Feb 07, 2010 7:46 am    Post subject: Reply with quote

Since you do the follwing in bóth calchyp and calcside, you might as well put it in your main function, right after you've asked the user for the sides. Then you can pass it as an extra argument to calchyp and calcside.
Code:
cout << "Please enter the number of decimals you'd like it to be rounded to." << endl;
cin >> rounding;


And system("pause"); is a bad practice. Instead you can use:
cin.sync();
cin.ignore();
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites