I found this function:
I would like to use this for my program.

void gotoxy(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

First of all, is the "COORD coord" neccesary? :/
and "GetStdHandle(STD_OUTPUT_HANDLE), coord)" What's this part? What does it really do?
I mean, get std handle... std handle is...?

Recommended Answers

All 4 Replies

First of all, is the "COORD coord" neccesary? :/

Presently, yes. In the next C++ standard there are alternatives though.

"GetStdHandle(STD_OUTPUT_HANDLE), coord)" What's this part? What does it really do?

Basically, it gets a handle you can use to write to the console. SetConsoleCursorPosition then uses that handle and the coordinates you specified to place the blinking cursor in your command prompt.

gotoxy is a C function that can move the text cursor to a different location on the screen... And with in the function is the SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

This is part of the gotoxy function it looks it is setting the position of a cursor...

This is the best i can tell you i am not familiar with C only C++

This functions sets cursor position to a praticular spot on the screen. Include <windows.h> and for example to move cursor to line 2, 10 letter write gotoxy(2,10). You dont have to worry about the code inside the function. Fore example if you want to make a countdown timer without writing number so many times

#include <iostream>
#include <windows.h>
using namespace std;
int getx() { //gets current console pos x
CONSOLE_SCREEN_BUFFER_INFO csbi;
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
   GetConsoleScreenBufferInfo(h,&csbi);
return csbi.dwCursorPosition.X;
}

int gety() { //gets current console pos y
CONSOLE_SCREEN_BUFFER_INFO csbi;
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
       GetConsoleScreenBufferInfo(h,&csbi);
    return csbi.dwCursorPosition.Y;
}


void gotoxy(int x, int y) //goes to x,y console
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}


int main() {
    int x,y;
int time = 10;

cout << "Time left ";
x = getx();
y = gety();

while(time>0) {
Sleep(1000);
gotoxy(x,y);
cout << time << "      \n";
time--;

}
}

I like to use it with function

void cursor(bool show) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO CursoInfo;
CursoInfo.dwSize = 1;         /* The size of caret */
CursoInfo.bVisible = show;   /* Caret is visible? */
SetConsoleCursorInfo(hConsole, &CursoInfo);
}

1 to show cursor and 0 to hide. SO they timer will look better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.