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 


My First 2 programs in C++
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
SkinnyQ
Master Cheater
Reputation: 0

Joined: 16 Jun 2006
Posts: 411

PostPosted: Tue Sep 04, 2007 12:06 pm    Post subject: My First 2 programs in C++ Reply with quote

Hi,
started C++ today.
My first programs are:
Miles to Kilometers convertor
Fahrenait to Celsius convertor.

Flame me for trying? Rolling Eyes
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Sep 04, 2007 12:13 pm    Post subject: Reply with quote

Nice. My only suggestion is to make functions for Farenheight to Celcius and vice versa and the same with your Miles to Kilometer thing. Then change your second if statement in both programs to if else statements. Next add an else statement that will say that they didn't select a valid option and then call your starting function that asks for their choice.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
SkinnyQ
Master Cheater
Reputation: 0

Joined: 16 Jun 2006
Posts: 411

PostPosted: Tue Sep 04, 2007 12:16 pm    Post subject: Reply with quote

oib111 wrote:
Nice. My only suggestion is to make functions for Farenheight to Celcius and vice versa and the same with your Miles to Kilometer thing. Then change your second if statement in both programs to if else statements. Next add an else statement that will say that they didn't select a valid option and then call your starting function that asks for their choice.

I understand. I would try to make another function\s.
Tho what do you mean by the vice versa.

EDIT: Here, I tryed to do what you said with the miles to km convertor.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Sep 04, 2007 12:29 pm    Post subject: Reply with quote

Vice versa. With the order or meaning reversed; conversely.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
SkinnyQ
Master Cheater
Reputation: 0

Joined: 16 Jun 2006
Posts: 411

PostPosted: Tue Sep 04, 2007 12:35 pm    Post subject: Reply with quote

oib111 wrote:
Vice versa. With the order or meaning reversed; conversely.

It does vice versa, I mean- converts Miles to kms and kms to miles.
And please tell me hows the new source code.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Sep 04, 2007 1:43 pm    Post subject: Reply with quote

I mean do a function for kms to mi and mi to kms.

Edit:

I edited it. I put in a infinite while loop. I changed your function name to invalid, put in some new lines here and there and deleted out conio.h, you really didn't need it. So heres the source + .exe file.

Code:

#include <iostream>

using namespace std;

int main()
{
    while(1) {
    int choice;
    void invalid();
    double miles, kilometers;
    cout << "This Program Converts Miles to Kilometers.\n";
    cout << "Press the option you want.\n\n";
    cout << "1 - Converting Kilometers to miles.\n";
    cout << "2 - Converting Miles to kilometers.\n";
    cout << "3 - Quit\n\n";
    cin >> choice;
    if(choice == 1)
    {
    cout << "\nHow many kilometers: \n\n";
    cin >> kilometers;
    miles = kilometers * 0.62;
    cout <<"\n\n"<< kilometers << " Kilometers " << "is\n\n" << miles << " Miles.\n\n";
    }
    else if(choice == 2)
    {   
    cout << "\nHow many miles: \n\n";
    cin >> miles;
    kilometers = miles * 1.61;
    cout <<"\n\n"<< miles << " Miles " << "is\n\n" << kilometers << " Kilometers.\n\n";
    }
    else if(choice == 3)
    {
         return 0;
    }
    else
    {
       invalid();
    }
}
    return 0;
}

void invalid()
{
    cout << "\nYou entered an invaild value.\n";
    cout << "Please Try again.\n\n";
    main();
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Tue Sep 04, 2007 3:42 pm    Post subject: Reply with quote

oib111, that is horrid.

1) for(;;) NOT while(1)!
2) Do not define variables in a loop!
3) That invalid function is useless and superfluous!
4) That chain of if/else statements would look much better as a swich.
5) Function looping is fun, but after a while the program will crash!
Code:
void invalid()
{
    cout << "\nYou entered an invaild value.\n";
    cout << "Please Try again.\n\n";
    main();  <------- FAIL!
}
Back to top
View user's profile Send private message
HomerSexual
Grandmaster Cheater Supreme
Reputation: 5

Joined: 03 Feb 2007
Posts: 1657

PostPosted: Tue Sep 04, 2007 3:45 pm    Post subject: Reply with quote

in my far - cel converter (win32 version) i used functions

look at my source

_________________
Back to top
View user's profile Send private message
DeltaFlyer
Grandmaster Cheater
Reputation: 0

Joined: 22 Jul 2006
Posts: 666

PostPosted: Tue Sep 04, 2007 3:50 pm    Post subject: Reply with quote

oib111 wrote:
I mean do a function for kms to mi and mi to kms.

Edit:

I edited it. I put in a infinite while loop. I changed your function name to invalid, put in some new lines here and there and deleted out conio.h, you really didn't need it. So heres the source + .exe file.


By doing what you did there, the loop serves no purpose at all since it would never reach its end. Making the "invalid" function is pointless in this case as you're only calling it once. Declare the function prototypes outside of other functions. Declare the variables outside of loops.

The big if-else structure could be replaced with a more readable switch-case structure. Instead of writing cout every time you want to start a new line to make your code more readable, you could simply remove the second cout and the ending semicolon on the previous line.

Edit: I type slow... Flyte got it first.

_________________

Wow.... still working at 827... what's INCA thinking?
zomg l33t hax at this place (IE only). Over 150 people have used it, what are YOU waiting for?
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Tue Sep 04, 2007 4:23 pm    Post subject: Reply with quote

Lol, I did this in like 5 seconds. I didn't think about what's best or whats not.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
TheSorc3r3r
I post too much
Reputation: 0

Joined: 06 Sep 2006
Posts: 2404

PostPosted: Wed Sep 05, 2007 7:59 am    Post subject: Reply with quote

Flyte wrote:
1) for(;Wink NOT while(1)!


who cares?

_________________


Don't laugh, I'm still learning photoshop!
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Wed Sep 05, 2007 8:05 am    Post subject: Reply with quote

TheSorc3r3r wrote:
who cares?


while(1) translates roughly to this

Code:

TestLoop:
mov eax, 1
cmp eax, 1
jne OutOfLoop
InLoop:
...
jmp TestLoop


for(;;) translates roughly to this

Code:

InLoop:
...
jmp InLoop


You see how much instructions you save? You are creating an unnecessary compare for something that is not logically sound. Ignore the fact that optimizing compilers will realize that 1 is equal to TRUE and will not do a compare- regardless of how it performs on the machine level it is always a pain to anyone reading your code: why did he compare 1 against 1? Is this developer clinically retarded? Should I bother to continue his work?
Back to top
View user's profile Send private message
zart
Master Cheater
Reputation: 0

Joined: 20 Aug 2007
Posts: 351
Location: russia

PostPosted: Wed Sep 05, 2007 8:14 am    Post subject: Reply with quote

It's just preference... Do I write while(1) in coding something I've been paid to develop? No - but for the most part I normally don't need an infinite loop and if I do I choose for(;Wink... It's just been instilled in me since those old college classes...

Do we really need to worry what it translates to or how fast/efficient each is? Not *really*... Unless your doing embedding programming you almost never have to worry about those kind of things for most (small) programs... Heck when do embedded some of the beasts are fast enough now though it doens't matter.

_________________
0x7A 0x61 0x72 0x74

TEAM RESURRECTiON
Back to top
View user's profile Send private message
appalsap
Moderator
Reputation: 0

Joined: 27 Apr 2006
Posts: 6753
Location: Pakistan

PostPosted: Wed Sep 05, 2007 8:18 am    Post subject: Reply with quote

It's not so much about speed as it is readability. It's like putting if(2 < 5) before every statement you want to execute unconditionally.
Back to top
View user's profile Send private message
zart
Master Cheater
Reputation: 0

Joined: 20 Aug 2007
Posts: 351
Location: russia

PostPosted: Wed Sep 05, 2007 8:23 am    Post subject: Reply with quote

appalsap wrote:
It's not so much about speed as it is readability. It's like putting if(2 < 5) before every statement you want to execute unconditionally.


Ehh... as for readability I'd say that while(1) is ugly - and for(;Wink is more pleasing to see, they both only take up three lines including there braces...

If I was debugging their code without the source then yes - it'd be ugly and I'd laugh a little (and die a little on the inside) - but it doesn't make it unreadable.

I'm not disagreeing with you app - I'm just saying it's not that crucial, especially if your not factoring in size/speed etc.

_________________
0x7A 0x61 0x72 0x74

TEAM RESURRECTiON
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