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 


Small coordinate program for cmd(C++)

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
educofu
Expert Cheater
Reputation: 3

Joined: 21 Aug 2009
Posts: 171
Location: Brazil,MG,OP

PostPosted: Wed Dec 01, 2010 10:42 am    Post subject: Small coordinate program for cmd(C++) Reply with quote

wrote this to learn the while loop and switch.

i think this is a good example of those.

also it may have some use in a little game etc...

Code:


#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
using namespace std;

void cs ()
{system("CLS");}

int random (int a,int b)
{
    if (b<a) return 0;
    int good;
    random:
    int r=rand();
    if (r>=a) good=1;
    else goto random;
    if (r<=b) good+=1;
    else goto random;
    if (good==2)return r;
    else return 0;
    }

int coor[16][16];

int x=8;
int y=8;
int action;
void move();
void print();

int main()
{
    coor[x][y]=1;
    cs();
    cout << "X: " << x << " Y: " << y << "\n";
    print();
    move(); 
    }
   
void move()
{
    cin >> action;
    switch(action)
    {
         case 2:
              coor[x][y]=0;
              y++;
              break;
         case 8:
              coor[x][y]=0;
              y--;
              break;
         case 6:
              coor[x][y]=0;
              x++;
              break;
         case 4:
              coor[x][y]=0;
              x--;
              break;
         }
    if (x<0)x=0;
    if (x>15)x=15;
    if (y<0)y=0;
    if (y>15)x=15;
    main();
    }
   
void print()
{
    int pX=0;
    int pY=0;
    while(pX<=16 && pY<=15)
    {
         cout << coor[pX][pY];
         pX++;
         if (pX==16)
         {
              cout << "\n";
              pX=0;
              pY++;
              }
         }
    }


suggestions and comments are welcome!

also, some 1 knows a good source to learn GUI C++?

_________________
"I finally started thinking outside of the box, only to find myself in a larger box."
Back to top
View user's profile Send private message MSN Messenger
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Dec 01, 2010 11:06 am    Post subject: Reply with quote

was gonna rewrite your program but got lazy. just changed one function:
Code:
int random (int a,int b) {
  assert( a < b );
  // use assert instead since 0 is potentially a valid return
  // assert is for debugging, I would advise sanitizing before calling
  // or returning an invalid value ( -1 ? )
  //if ( b < a ) {
  //  return 0;
  //}
 
  // assuming b - a < RAND_MAX
  return a + ( rand() % ( b - a ) );
}


your code is a horrible example of 'c++'. you make absolutely no use of the OOP paradigm. your code is basically unreadable. it is counter-intuitive, messy and you hardcode in magic numbers without comments or #define. the style of your programming is an open invitation for logic bugs as well as providing next to no readability value.

variables that do not have to be global should be localised to a function scope and if they are needed elsewhere, should be passed as parameters. this decouples code and breaks the dependencies you have created. it is in general, bad style to call main().

don't get started on gui code until you understand basic C++.
Back to top
View user's profile Send private message
educofu
Expert Cheater
Reputation: 3

Joined: 21 Aug 2009
Posts: 171
Location: Brazil,MG,OP

PostPosted: Wed Dec 01, 2010 12:30 pm    Post subject: Reply with quote

that function shouldnt be there... (i always copy n paste the start)

also, that random function i made a long time ago when i was learning, and it works fine.

_________________
"I finally started thinking outside of the box, only to find myself in a larger box."
Back to top
View user's profile Send private message MSN Messenger
Slugsnack
Grandmaster Cheater Supreme
Reputation: 71

Joined: 24 Jan 2007
Posts: 1857

PostPosted: Wed Dec 01, 2010 12:54 pm    Post subject: Reply with quote

it does not work fine. it potentially loops forever.
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