   [c++] Text-Based Movement Code
  Hi guys, can you please critique my code for just moving an @ around a box? It works fine now, but I think when I start trying to implement items, checking to see if the character is in certain positions will get too complex compared to say, a 2d array. But I don't really know much about this kind of stuff. Any suggestions? using namespace std;const char a='@';//function to set cursor position to certain spotvoid gotoxy( int column, int line ){COORD coord;coord.X = column;coord.Y = line;SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );}//function which moves the character in a mapvoid move_char() {//initialises the up and across variablesint up=1;int across=1;//const char for the loopchar x='n';gotoxy(0,0);cout<<"#########################################"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#    #"<<endl;cout<<"#########################################"<<endl;//20 down//40 across//loop of movementdo {gotoxy(across, up);std::cout<<a;//go to below the field and get the inputgotoxy(0, 21);char input=_getch();gotoxy(across, up); cout<<" "<<endl;//checking if the character has to be movedif (input=='w') {up--;}if (input=='d') {across++;}if (input=='s') {up++;}if (input=='a'){across--;}//hotkeysif (input=='q')  exit(0);if (input=='m')  menu();//just in case the user tries to exit the fieldif (up==0) up++;if (up==20) up--;if (across==0) across++;if (across==40) across--;} while (x!='q');}Thanks, Darkcrobat[Edited by - darkcrobat on March 31, 2010 7:09:22 PM]
  A few things to look at:1. The width and height of the box shouldn't be numbers through out the code. The value of these should be set once and then the variable or constant used throughout.2. The box each shouldn't really be printed out staticly with the couts, it should be displayed based on the width and height that has been defined. You may want to figure out how to use a loop/s to display the box.3. As far as I can tell the variable x (generally a bad name for a variable unless dealing with coordinates) gets set to 'n' and then never changed so the condition for you while loop is the same as while (true) <-- easier to read this wayYou are correct in thinking that doing things the way you currently are will make things more difficult for adding items, enemies etc. and would be better finding another way to represent the box and an array would be a good place to start. ;  Okay, thanks. So is it legal to just put do { //...}while(true) If so, I would do that, but the old C-style (&&) doesn't seem to work in my compiler (vc++)   ;  Yes, while(true) is legal.What I think your trying to do though should actually be:while (input != 'q' && input != 'Q')orwhile (!(input == 'q' || input == 'Q'))this will make it so that when someone presses q the loop will end and the program will continue on and eventually end.Which means you won't need to have the following line anymore:if (input=='q')  exit(0);   