Erase what you just printed in a while loop

This is a discussion on Erase what you just printed in a while loop within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <conio.h> int main() { int x = 0; //Horizontil coordinates int y = 0; //Vertical coordinates ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    100

    Erase what you just printed in a while loop

    Code:
    #include <iostream>
    #include <conio.h>
    
    int main()
    {
      int x = 0; //Horizontil coordinates
      int y = 0; //Vertical coordinates
      int whereToGo; 
      while (whereToGo != 27)
      {
      for (int i = 0; i < y; i++)
      {
      cout << "\n";
      }
      for (int i = 0; i < x; i++)
      {
      cout << " ";
      }
      cout << "*";
      whereToGo = getch();
      if(whereToGo == 80)//if press down arrow then make y = 1 + y
      {
      y++;
      }
      if(whereToGo == 72)//if press up arrow then make y = y - 1
      {
      y--;
      }
      if(whereToGo == 77)//if press right arrow then make x = 1 + x
      {
      x++;
      }
      if(whereToGo == 75)//if press left arrow then make x = x - 1
      {
      x--;
      }
      }
      return 0;
    }
    I am making a simple program that makes it look like the little * is moving. but after whereToGo = getch() i need it to erase what it print before that... do you understand what i mean?... please help

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Perhaps clrscr() would work.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    100
    do you have to include a header file for the function? its not working

  4. #4
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    >> do you have to include a header file for the function? its not working

    The required header file is <conio.h> which you have already included...

    The problem is in the position where you have place your clrscr(); try a different position in the same code bit.

    I'm not testing it, but perhaps this should work...

    #include <iostream>
    #include <conio.h>

    int main(){

    int x = 0;
    int y = 0;

    int whereToGo;

    while (whereToGo != 27) {
    for (int i = 0; i < y; i++) {
    cout << "\n"; } for (int i = 0; i < x; i++) { cout << " "; }
    cout << "*"; whereToGo = getch();

    clrscr();

    if(whereToGo == 80)//if press down arrow then make y = 1 + y { y++; }
    if(whereToGo == 72)//if press up arrow then make y = y - 1 { y--; }
    if(whereToGo == 77)//if press right arrow then make x = 1 + x { x++; }
    if(whereToGo == 75)//if press left arrow then make x = x - 1 { x--; } }
    return 0;}

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    100
    it still isnt working

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    21
    Is the clrscr() function not working? If the problem is that, it can depend on your compiler. (Maybe it's not supporting clrscr())
    Indicate your error(s) so others can understand the problem.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    100
    im useing dev-C++ and it says:
    implicit declaration or function "int clrscr(...)"

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    21
    Try system("cls")
    Code:
    #include <iostream> 
    #include <conio.h>
    using namespace std;
    int main(){ 
    
    int x = 0; 
    int y = 0; 
    
    int whereToGo; 
    
    while (whereToGo != int('q')) 
    { 
    for (int i = 0; i < y; i++) 
    	cout << "\n"; 
    for (int i = 0; i < x; i++) 
    	cout << " ";  
    cout << "*"; 
    whereToGo = getch(); 
    
    system("cls");
    if(whereToGo == 80)     y++;
    if(whereToGo == 72)     y--; 
    if(whereToGo == 77)     x++;
    if(whereToGo == 75)     x--; 
    }
    return 0;
    }

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    100
    that works... but it is all skippy and stuff... try it and youll see

    Code:
    #include <iostream>
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    using namespace std;
    
    void topframe (int,int,int,int);
    void gotoxy(int,int);
    
    int main()
    {
      HANDLE h;
      h = GetStdHandle ( STD_OUTPUT_HANDLE ); 
      SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE ); 
      int x = 0; //Horizontil coordinates
      int y = 0; //Vertical coordinates
      int whereToGo;
    
      while (whereToGo != 27)
      {
        topframe(1,1,77,3); 
      cout << "\n\n";
      for (int i = 0; i < y; i++)
      {
      cout << "\n";
      }
      for (int i = 0; i < x; i++)
      {
      cout << " ";
      }
      putchar(2);
      whereToGo = getch();
      system("cls");
      if(whereToGo == 80)//if press down arrow then make y = 1 + y
      {
      y++;
      }
      if(whereToGo == 72)//if press up arrow then make y = y - 1
      {
      if(y == 0)
      {
      //do nothing
      }
      else
      {
      y--;
      }
      }
      if(whereToGo == 77)//if press right arrow then make x = 1 + x
      {
      x++;
      }
      if(whereToGo == 75)//if press left arrow then make x = x - 1
      {
      x--;
      }
      }
      return 0;
    }
    
    void topframe (int xmin, int ymin, int xmax, int ymax)//red_baron's code
    {
       HANDLE h;
       h = GetStdHandle ( STD_OUTPUT_HANDLE );
       int count;
       gotoxy(xmin,ymin);
       putchar(218);
       gotoxy(xmax,ymin);
       putchar(191);
       gotoxy(xmin,ymax);
       putchar(192);
       gotoxy(xmax,ymax);
       putchar(217);
       gotoxy(xmin+1,ymin);
       for(count=xmin; count<xmax-1; count++)
           putchar(196);
           gotoxy(xmin+1,ymax);
       for(count=xmin; count<xmax-1; count++)
          putchar(196);   
          
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmin,count);
          putchar(179);
       }   
       SetConsoleTextAttribute ( h, FOREGROUND_GREEN );
       cout << "The text goes here.";
       SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
       for(count=ymin+1; count<=ymax-1; count++)
       {
          gotoxy(xmax,count);
          putchar(179);
       }
    }
    
    void gotoxy(int x, int y)
    {
    	HANDLE hConsoleOutput;
    	COORD dwCursorPosition;
    
    	dwCursorPosition.X = x;
    	dwCursorPosition.Y = y;
    	hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
    }

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Can you post the last ver.,

    Please could you post the last version of your work, and can you tell how many problems you still have ?
    C++
    The best

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    100
    i just did... my onley prob is the it skips too much... i need a different way that doesnt skip

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    317
    After you write the '*' to the screen and prior to writing the new one, why dont you try just putting a space to the old cursor position and then just writing a new '*' to the new cursor position. Then get rid of the system("cls"); function. That should do it. I believe space is char(32), I'm not sure though.

  13. #13
    Registered User
    Join Date
    Jun 2002
    Posts
    29
    It seems good, using ' '(space) instead of system("cls");.

    Maybe it will be better to use one switch instead of four if statements, it will be much more readable..
    (and also shorter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about erase()
    By Sharke in forum C++ Programming
    Replies: 8
    Last Post: 06-10-2009, 02:22 AM
  2. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  3. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM
  4. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 09:15 AM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21