gotoxy question

This is a discussion on gotoxy question within the C Programming forums, part of the General Programming Boards category; I have made a program that moves a dot around the screen. The program doesn't work right all the time(leaves ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    gotoxy question

    I have made a program that moves a dot around the screen. The program doesn't work right all the time(leaves dots on the screen) unless I have a certain gotoxy function in. When I look through the program, the function doesn't appear necessary, but it makes all the difference to my compiler.

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>

    #define ENTER '\r'
    #define UP 72
    #define DOWN 80
    #define LEFT 75
    #define RIGHT 77

    main()
    {
    int key,xval,yval;

    xval=40;
    yval=12;
    key=0;

    clrscr();
    printf("Use the arrow keys to move the dot, ENTER to quit: ");
    gotoxy(xval,yval);
    printf("*");

    while(key!=ENTER) {
    key=getch();
    if(key==0) /*this statement blocks other input*/
    key=getch();
    else
    break;
    gotoxy(xval,yval); /*this line is the key*/
    printf(" ");

    switch(key) { /*moving the cursor*/
    case UP:
    gotoxy(xval,--yval);
    break;
    case DOWN:
    gotoxy(xval,++yval);
    break;
    case LEFT:
    gotoxy(--xval,yval);
    break;
    case RIGHT:
    gotoxy(++xval,yval);
    break;
    }
    printf("*");
    }
    }


    can anybody tell me why this happens??

    p.s. are these tags good enough prelude?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > p.s. are these tags good enough prelude?

    Hehe, no. You have to use [code] to start your block of code, and [/c...] to end it. (where c... is actually the word code)

    > gotoxy(xval,yval); /*this line is the key*/
    > printf(" ");

    Of course it's the key. What you're doing is simply writing a space over top of the current star. Remember: when you put anything on the screen, it advances the cursor one place. Without moving back to x/y, you'd be writing the space just PAST the star. So, you have to go back to where the star is, then overwrite it with a space.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    51
    If you didn't have the gotoxy() there, it would print a space right after the original position. The gotoxy moves it back so that the space prints over the '*' and not after it.


    ::EDIT:: Sorry, Quzah got it while I was posting.
    Last edited by drharv; 04-24-2002 at 08:36 PM.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    thanks. that sounds familiar......and I forgot about the cursor advancing.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I posted code for this very problem about a week ago:
    http://www.cprogramming.com/cboard/s...ghlight=gotoxy

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    <conio.h>

    I don't know about your compiler but i use gcc from DJGPP you need to include <conio.h> to use gotoxy();
    Ünicode¬>world = 10.0£

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  2. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 12:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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