Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: clrscr() and gotoxy() in gcc

  1. #1
    Join Date
    Aug 2007
    Location
    Taguig City, Philippines
    Beans
    94
    Distro
    Ubuntu 7.04 Feisty Fawn

    Post clrscr() and gotoxy() in gcc

    clrscr()
    just add the following to your code to clear the screen first before executing your program.

    #include<stdlib.h>
    main()
    {
    system("clear"); //clears the screen
    }
    gotoxy()
    just add the following to your code to use the gotoxy() function, which changes the position of the cursor to the desired coordinates of your screen.

    #include<stdio.h>
    //gotoxy function
    void gotoxy(int x,int y)
    {
    printf("%c[%d;%df",0x1B,y,x);
    }
    main ()
    {
    gotoxy(25,50); //reposition cursor
    printf("hello world"); //display text
    }

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: clrscr() and gotoxy() in gcc

    Would it not be easier to use ncurses? All of the "work" has been done to provide one the capability to manipulate the position of the cursor (and output) within an xterm.

    $ man ncurses

  3. #3
    Join Date
    Aug 2005
    Location
    The Local Group
    Beans
    631

    Re: clrscr() and gotoxy() in gcc

    Code:
    xxxxxxx@xxxx:temp> a.out
                            hello worldxxxxxxx@xxxx:temp>
    Is that what program #2 was supposed to do?

    And you really should look into ncurses. It's much more flexible than anything you can hack together with system() and printf().

  4. #4
    Join Date
    Aug 2007
    Location
    Canada
    Beans
    459
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: clrscr() and gotoxy() in gcc

    I think his two functions are much better than use a library, anyway why two use a library just for two functions... Make a code depending of one library just for a couple of functions does not have any sense...!!!

    Maybe the functions are not working completely correct but they will work better after fix some bugs...

    Thanks for the functions mitchi...
    Best, Gnusci

    "Never make a calculation until you know the answer." -- Wheeler, Spacetime Physics, pg 60.

  5. #5
    Join Date
    Mar 2011
    Beans
    2

    Smile Re: clrscr() and gotoxy() in gcc

    Quote Originally Posted by mitchi View Post
    clrscr()
    just add the following to your code to clear the screen first before executing your program.



    gotoxy()
    just add the following to your code to use the gotoxy() function, which changes the position of the cursor to the desired coordinates of your screen.
    thanks it works, i have tried to this

  6. #6
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: clrscr() and gotoxy() in gcc

    Instead of using system to call an external program, you could also use this:
    Code:
    fprintf(stdout, "\033[2J\033[0;0f");
    fflush(stdout);

  7. #7
    Join Date
    Jul 2011
    Beans
    12

    Re: clrscr() and gotoxy() in gcc

    TRY THIS ONE
    #include<stdio.h>
    #include<stdlib.h>
    int lasti=0,lastj=0;
    void goback(int x,int y)
    {
    printf("\033[%dA",x);
    printf("\033[%dD",y);
    }
    void gotoxy(int x,int y)
    {
    extern int lasti, lastj;
    goback(lasti, lastj);
    int i;
    for(i=0;i<x;i++)
    printf("\n");
    for(i=0;i<y;i++)
    printf("\t");
    lasti=x;
    lastj=y;

    }

    int main ()
    {
    int i=0;
    while(1)
    { i=i+1;
    gotoxy(12,5);
    printf("hello i=%d\r",i); //display text
    gotoxy(10,5);
    printf("hello i-1=%d\r",i-1);
    usleep(3*1000);
    }
    }

  8. #8
    Join Date
    May 2013
    Beans
    2

    Re: clrscr() and gotoxy() in gcc

    Hi,
    I have tried above code, I am able to change y position in screen but I am not able to change x position, i am passing different x value then then also my x value is 0 only.

  9. #9
    Join Date
    May 2013
    Beans
    2

    Re: clrscr() and gotoxy() in gcc

    Hi,
    I tried above code but I am not able to move x position, I am passing different value then also x position is 0 only.

  10. #10
    Join Date
    Jun 2010
    Location
    Loznica Serbia
    Beans
    126
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: clrscr() and gotoxy() in gcc

    The gotoxy and clrscr are functions from Windows C library. They are not available on Linux.
    Windows is not user friendly,it's just user familiar

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •