Thread: figure - problem

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    figure - problem

    I have wroten programm which draw a figure. We can move figure by cursors. I would like to create a new function move_and_control but then my figure stay in one place and don't move. I public my code:

    Code:
    #include <iostream>
    #include <conio>
    
    using namespace std;
    
    void start ();
    void get_sign(char &znak);
    void get_size (int &i, int &j, char znak);
    void draw (int i, int j, char znak);
    
    
    void main()
    {
      int i,j;
      char znak;
    
      _setcursortype (_NORMALCURSOR);
      start ();
      get_sign(znak);
      get_size(i, j, znak);
      _setcursortype (_NOCURSOR);
      draw ( i, j, znak);
    }
    
    
    //************************* FUNCTIONS **********************************
    void start ()
       {
       cout<<"Programm draw an ASCII figure:\n";
       cout<<"      **********\n";
       cout<<"      *         *\n";
       cout<<"      *          *\n";
       cout<<"      *         *\n";
       cout<<"      **********\n\n";
       cout<<"    -= Hit any key =-";
       getch();
    }
    
    //******************************************************************************
    void get_sign(char &znak)
    {
       int x,y;
       clrscr ();
       cout<<"     Choose an ASCII and ENTER,\n";
       cin>>znak;
    }
    //******************************************************************************
    void get_size (int &i, int &j, char znak)
    {
    
      int min_szer=3, max_szer=40, min_wys = 3, max_wys, er, x, y;
      clrscr();
    
      cout<<"Choose an width from 3 to 40: ";
        x = wherex(); y = wherey();
       do
       {
         gotoxy(x,y);
         clreol();
         er = 0;
         cin >> i;
         if (cin.fail())              // if(!cin)          //control of wrong date
           {
             er = 1;
             cin.clear();
             cin.ignore(0xff, (int)'\n');
           }
        }
        while ((i < min_szer)||(i > max_szer) || er );
    
      clrscr();
    
      if(i>=23) max_wys = 23;
            else max_wys = i;
        
    
      cout<<"Choose a high  (odd number) from 3 to "<<max_wys<<": ";
          x = wherex(); y = wherey();
         do
       {
          gotoxy(x,y);
         clreol();
         er = 0;
         cin >> j;
         if (cin.fail())              // if(!cin)                //control of wrong date
           {
             er = 1;
             cin.clear();
             cin.ignore(0xff, (int)'\n');
           }
        }
        while ((j < min_wys)||(j > max_wys) || er || j%2==0);
    }
    
    //******************************************************************************
    
    void move_and_control ( int poziom, int pion, int &g, int aa, int bb, int i, int j);
    //******************************************************************************
    void  draw (int i, int j, char znak)
    {
      int g,x,y,z;
      int poziom=0;
      int pion=0;
      int ppoziom=0, ppion=0;
      int aa = 40;
      int bb = 11;
    
      clrscr();
    
    //========== DRAW A LEGEND ============
               gotoxy(1,24);
               for(x=1;x<=80;x++)cout<<"=";
               cout<<" Move a figure by cursors   ||   Esc = exit";
    
      do{
    //========== DRAW A FIGURE =============
             for(x=1;x<=(i-j/2);x++)               // draw an up edge
             {
                gotoxy((aa-i+x+poziom),(bb-(j/2))+pion+1);
              cout<<znak;
             }
    
             for(x=(i-j/2);x<i;x++)                // draw a sloping up edge
             {
               z = wherey();
               gotoxy ((aa-i+1+poziom),z);
               cout << znak;
               gotoxy ((aa-i+x+poziom), z);
               cout << znak << endl;
             }
    
             z=wherey();                          //draw a center
             gotoxy ((aa-i+1+poziom),z);
             cout<<znak;
             gotoxy ((aa-i+x+poziom),z);
             cout<< znak;  /*<< endl; */
             if( (aa-i+x+poziom)< 80) cout<<endl;
    
    
              for (x=(i-1);x>(i-(j+1)/2+1);x--)    //draw a sloping dwon edge
             {
               z = wherey();
               gotoxy ((aa-i+1+poziom),z);
               cout<< znak;
               gotoxy (aa-i+x+poziom, z);
               cout<< znak << endl;
             }
    
              for(x=1;x<=(i-j/2);x++)               // draw a down edge
             {
               z=wherey();
               gotoxy((aa-i+x+poziom),z);
               cout<<znak;
             }
    
             ppion = pion;
             ppoziom = poziom;
    
    move_and_control( poziom, pion, g, aa, bb, i, j);
    
    
    //========= CLEAN A FIGURE ===========
    
             for(x=1;x<=(i-j/2);x++)                
             {
                gotoxy((aa-i+x+ppoziom),(bb-(j/2))+ppion+1);
              cout<<" ";
             }
    
             for(x=(i-j/2);x<i;x++)              
             {
               z = wherey();
               gotoxy ((aa-i+1+ppoziom),z);
               cout << " ";
               gotoxy ((aa-i+x+ppoziom), z);
               cout << " " << endl;
             }
    
             z=wherey();                         
             gotoxy ((aa-i+1+ppoziom),z);
             cout<<" ";
             gotoxy ((aa-i+x+ppoziom),z);
             cout<< " ";
             if( (aa-i+x+ppoziom)< 80) cout<<endl;
    
              for (x=(i-1);x>(i-(j+1)/2+1);x--)     
             {
               z = wherey();
               gotoxy ((aa-i+1+ppoziom),z);
               cout<< " ";
               gotoxy (aa-i+x+ppoziom, z);
               cout<< " " << endl;
             }
    
              for(x=1;x<=(i-j/2);x++)             
             {
               z=wherey();
               gotoxy((aa-i+x+ppoziom),z);
               cout<<" ";
             }
        }while(g!=27);                              // ESC
    }
    //******************************************************************************
    
    void move_and_control ( int poziom, int pion, int &g, int aa, int bb, int i, int j)
    {
    
            g=getch();
            if (g==0)
            {
              g=getch();
    
              if(g==75)                               //control and move left
              {
                if(aa-i+poziom > 0) poziom = poziom--;
              }
    
               if(g==77)                              //control and move right
              {
                  if (aa-i+poziom  < aa+aa - i ) poziom = poziom++;
              }
    
              if(g==72)                               //control and move up
              {
                   if ( (bb-(j/2)+pion) > 0) pion=pion--;
              }
    
              if(g==80)                               //control and move down
              {
                   if ( (bb-(j/2)+pion-1) < bb+bb -j) pion = pion++;
              }
            }
    
    }
    
    /*----------------------------------------------------------------------------*/
    Last edited by jamesfisher; 11-09-2009 at 10:57 PM.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Like g, poziom and pion need to be references if you want to change the values the passed arguments hold outside the function.

    On a different note, you don't need to post so much code. Next time, just post the function giving you trouble, and where you are using it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Quote Originally Posted by King Mir View Post
    Like g, poziom and pion need to be references if you want to change the values the passed arguments hold outside the function.
    A have change for a moment code -> &poziom , &pion

    but there is this same problem and figure don't move.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Are you sure that g==0 is the correct value for the control? I recall it being something closer to 127.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    I have resolved the problem. I changed pluses and minuses in incrementation and decrementation < e.g. from "poziom = poziom--" to "poziom = --poziom".
    My code:
    Code:
    void przesuwanie_i_kontrola (int &poziom,int &pion, int &g, int aa, int bb, int i, int j)
    {
    
            g=getch();
            if (g==0)
            {
              g=getch();
    
              if(g==75)                               //kontrola i przesunięcie w lewo
              {
                if(aa-i+poziom > 0) poziom = --poziom ;
              }
    
               if(g==77)                              //kontrola i przesunięcie w prawo
              {
                  if (aa-i+poziom  < aa+aa - i ) poziom = ++poziom;
              }
    
              if(g==72)                               //kontrola i przesunięcie w góre
              {
                   if ( (bb-(j/2)+pion) > 0) pion=--pion;
              }
    
              if(g==80)                               //kontrola i przesunięcie w dół
              {
                   if ( (bb-(j/2)+pion-1) < bb+bb -j) pion = ++pion;
              }
           }
    
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No that's wrong. Use either --poziom or poziom = poziom-1. -- already changes the variable, and it's illegal to change the same variable twice in the same expression.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    true . I changed -> "--poziom"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM