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 


Shitty C++ Calc
Goto page 1, 2  Next
 
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 Aug 23, 2009 4:09 am    Post subject: Shitty C++ Calc Reply with quote

Learning C++ from a 15 y/o book.

Code:
#include <iostream>

using namespace std;

void add(float x,float y);
void sub(float x,float y);
void mult(float x,float y);
void div(float x,float y);

int main()
{
    char selection;
    int ans;
    float x;
    float y;

    cout << "Enter your first number here: ";
    cin >> x;
    cout << "Enter your second number here: ";
    cin >> y;

    cout << "Do you want to add, subtract, multiply or divide these numbers?" << endl;
    cout << "Type a for add, s for subtract, etc." << endl;
    cin >> selection;

    switch(selection) {
        case 'a':
        add(x, y);
        break;

        case 's':
        sub(x, y);
        break;

        case 'm':
        mult(x, y);
        break;

        case 'd':
        if (y == 0)
        cout << "You divided by 0." << endl;
        else div(x, y);
        break;

        default:
        cout << "You selected none of the choices." << endl;

    }

    system("pause");
    return 0;
}

void add(float x, float y)
{
    cout << (x+y) << endl;
}

void sub(float x, float y)
{
    cout << (x-y) << endl;
}

void mult(float x, float y)
{
    cout << (x*y) << endl;
}

void div(float x, float y)
{
    cout << (x/y) << endl;
}
Back to top
View user's profile Send private message
Zerith
Master Cheater
Reputation: 1

Joined: 07 Oct 2007
Posts: 468

PostPosted: Sun Aug 23, 2009 5:08 am    Post subject: Reply with quote

...what are you trying to show here?
Back to top
View user's profile Send private message MSN Messenger
Jani
Grandmaster Cheater
Reputation: 2

Joined: 29 Dec 2006
Posts: 804

PostPosted: Sun Aug 23, 2009 5:11 am    Post subject: Reply with quote

Zerith wrote:
...what are you trying to show here?
Holland wrote:
Shitty C++ Calc
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 5:19 am    Post subject: Reply with quote

there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least

of course those differences are minimal but it is not good code practice to do as you have done
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sun Aug 23, 2009 6:29 am    Post subject: Reply with quote

Slugsnack wrote:
there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least

of course those differences are minimal but it is not good code practice to do as you have done


Or use __inline, but that's microsoft specific. But of course, the compiler might optimize it to inline for you.
Back to top
View user's profile Send private message
sponge cake recipe
Grandmaster Cheater Supreme
Reputation: 22

Joined: 24 Sep 2007
Posts: 1635

PostPosted: Sun Aug 23, 2009 6:44 am    Post subject: Reply with quote

Slugsnack wrote:
there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least

of course those differences are minimal but it is not good code practice to do as you have done

I was told hardcoding was bad, perhaps not for this, but it's good to get into the habit of creating separate functions, so if they're called multiple times it'll be better?
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Sun Aug 23, 2009 7:18 am    Post subject: Reply with quote

Holland wrote:
Slugsnack wrote:
there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least

of course those differences are minimal but it is not good code practice to do as you have done

I was told hardcoding was bad, perhaps not for this, but it's good to get into the habit of creating separate functions, so if they're called multiple times it'll be better?


What compiler are you using?

If you're using VC++, google the '__inline' specifier.

What it does is instead of compiling as call/ret, it will just put the compiled asm right "in-line" where it is called, hence speeding up, good coding practice, etc.
Back to top
View user's profile Send private message
sponge cake recipe
Grandmaster Cheater Supreme
Reputation: 22

Joined: 24 Sep 2007
Posts: 1635

PostPosted: Sun Aug 23, 2009 8:37 am    Post subject: Reply with quote

smartz993 wrote:
Holland wrote:
Slugsnack wrote:
there is not much point in using functions if the content of a function is a 1 liner and it is only called once. it will just slow your program down and depending on how your compiler is set will make the code slower because of superfluous functions. indeed a call/ret is needed at the least

of course those differences are minimal but it is not good code practice to do as you have done

I was told hardcoding was bad, perhaps not for this, but it's good to get into the habit of creating separate functions, so if they're called multiple times it'll be better?


What compiler are you using?

If you're using VC++, google the '__inline' specifier.

What it does is instead of compiling as call/ret, it will just put the compiled asm right "in-line" where it is called, hence speeding up, good coding practice, etc.

Code::Blocks.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 10:17 am    Post subject: Reply with quote

it is good practice to use functions when they are called multiple times but in this case they are not so i don't see the point in writing them as such

anyway.. not sure your purpose of posting this here. i am guessing for us to critique it ?

something i would change is when you have your IF conditions to indent the next line by 2 spaces or a tab or something to make it stand out more

other than that, code something more advanced, come back and post that instead
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sun Aug 23, 2009 10:23 am    Post subject: Reply with quote

You can do...
Code:

using namespace std::cout;
using namespace std::cin;


instead of using the whole std namespace.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 10:33 am    Post subject: Reply with quote

uhh what would be the point in doing that ? gains no particular advantage

namespace is just a compiler directive so at runtime there is no difference and i doubt very much whether even at compiletime there is a difference either

on the other hand, he could just save an extra unnecessary line
Back to top
View user's profile Send private message
TiMBuS
Cheater
Reputation: 0

Joined: 27 Jun 2006
Posts: 44

PostPosted: Sun Aug 23, 2009 10:38 am    Post subject: Re: Shitty C++ Calc Reply with quote

Here i added some killer optimizations to yr code check it:

Code:
int add(int x, int y)
{
    if (y == 0)
        return x;
    return add(++x, --y);
}

int sub(int x, int y)
{
    if (y == 0)
        return x;
    return sub(--x, --y);
}

int mult(int x, int y)
{
    if (y == 0)
        return 0;
    return add(x, mult(x, sub(y, 1)));
}

int div(int x, int y)
{
    if (sub(x, y) < 0)
        return 0;
    return add(div(sub(x, y), y), 1);
}


only works with ints tho...

_________________
Build something even a fool can use and only a fool will use it.
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 10:44 am    Post subject: Reply with quote

possibly the mult/div would perform better than classical *// but for the first 2 i can't see them as anything other than potentially highly recursive

still code like this does serve a good educational purpose for a beginner, good job

also a little preference, in case OP does not know, instead of 'if (y == 0)', one can write 'if ( !y )' which i prefer but that is a matter of taste and style
Back to top
View user's profile Send private message
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Sun Aug 23, 2009 11:28 am    Post subject: Reply with quote

yes because i obviously did not spot the sarcasm. be less presumptious, x0r

on second thoughts i'll let you continue rimming ass
Back to top
View user's profile Send private message
&Vage
Grandmaster Cheater Supreme
Reputation: 0

Joined: 25 Jul 2008
Posts: 1053

PostPosted: Sun Aug 23, 2009 1:16 pm    Post subject: Reply with quote

Slugsnack wrote:
uhh what would be the point in doing that ? gains no particular advantage

namespace is just a compiler directive so at runtime there is no difference and i doubt very much whether even at compiletime there is a difference either

on the other hand, he could just save an extra unnecessary line

http://ubuntuforums.org/showthread.php?t=595176

I tend to be more explicit on things.


& You tend to like to attempt to troll me on every post that I've post, is it to make you look superior over the interwebz, or is it compensation for the lame life you are currently living?
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
Goto page 1, 2  Next
Page 1 of 2

 
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