console cursor position

closed account (28poGNh0)
Hello everyone this is my first contribution in this website ..
I am looking for a function or manner to know everytime the function being called the position of the cursor like this way

int main()
{
cout << "The position now is : " << get_position() << endl;
cout << "and now is : " << get_position() << endl;
}

thanks a lot for reading
You can manipulate this program of mine.

// Joshua D. Haglund
// July 2nd 2012
// Fetch Mouse Location

#include <windows.h>
#include <winuser.h>
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

int x,y,w,z,c;
c=0;

while(c!=10)
{
mouse_event(MOUSEEVENTF_MOVE, x, y,w,z);
POINT pt;
GetCursorPos(&pt);
x=pt.x;
y=pt.y;
cout << x << "," << y << endl;
Sleep(1000);
x=0;
y=0;
c=c+1;
}
system("pause");
return 0;
}
closed account (28poGNh0)
Thank you so much both of you toneewa and Duoas.

I think you're close to understand my problem which is

when I call "get_position()"-function of mine-I expect to get the current position of the cursor and I hope the output of my program will like this :

The position now is : 22,0
and now is : 13,1

22,0 means the line 0 ,the cursor at the caractere 22

thans for reading ,great to meet you guys
@ Duoas: How is it that with over 6000 posts you are still able to link back to entries you made almost 4 YEARS AGO?!?!?! Never-mind how you even remembered you made it, I'll just assume you have an eidetic memory in that regard.
LOL. That, and Google helps me find stuff when I can't be bothered. :O)

(Also, I do tend to remember the kinds of posts I've made several hundred times over. Which is easy, as I tend to post what I know about anyway... Alas, as Holmes said, "you know a conjurer gets no credit when once he has explained his trick".)
closed account (28poGNh0)
Please can you help me out ?
Wait... there's another question in there?
@ Duoas: Maybe the OP has ESL and is having trouble reading your code\post?

@ OP: The link that Duoas provided gives you three functions, (NOTE: This will only work in windows). The ones you are interested in are "wherex()" at Lines 14-24 and "wherey() at Lines 26-36. "wherex()" will give the the X coordinates of the cursor, or how many character spaces it is in from the left side of the screen. The "wherey()" function gives you the Y coordinates, or how many lines from the top of the screen the cursor is.

Now for your purposes you can simplify his code and just call "GetScreenBufferInfo()" once. The CONSOLE_SCREEN_BUFFER_INFO struct pointed to in the second argument has a memeber called "dwCursorPosition" that will hold both the X and Y coordinates once "GetScreenBufferInfo()" returns.
closed account (28poGNh0)
Hey Computergeek01 hey others

I had tried that again befor you told me but I did find what I am looking for

I call "wherx()" function at this way cout << "Hello again" << wherx() ;

but I just got huge number I delete couple of letters ,I still have the same number

I hope not to bother you ,thanks
closed account (28poGNh0)
please any news I need it so bad
Do you really want something like GetConsoleScreenBufferInfo() ?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171%28v=vs.85%29.aspx
Last edited on
Oh wow, there seems to be a glaring mistake in Duoas's code. You want to return the variable "csbi.dwCursorPosition.X" and "csbi.dwCursorPosition.Y" not "result", result doesn't hold anything. I assume he meant to assign the COORD variables to "result" at some point but it's not done here in his example.
Dang, you're right!

//me feels stupid...

Return csbi.dwCursorPosition.X (or Y).
closed account (28poGNh0)
I am sorry I still dont get it
can you give a complete exemple if that is possible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <windows.h>

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

int wherex()
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  if (!GetConsoleScreenBufferInfo(
         GetStdHandle( STD_OUTPUT_HANDLE ),
         &csbi
         ))
    return -1;
  return csbi.dwCursorPosition.X;
  }

int wherey()
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  if (!GetConsoleScreenBufferInfo(
         GetStdHandle( STD_OUTPUT_HANDLE ),
         &csbi
         ))
    return -1;
  return csbi.dwCursorPosition.Y;
  }
closed account (28poGNh0)
I forgot to say thanks a lot guys
specially you Duoas
Topic archived. No new replies allowed.