SetConsoleCursorPosition Function

Sep 18, 2008 at 3:06pm
I was looking within the past few weeks for a true simple example of the cursor position function use, these forums seems to be my last stop before giving up.

I've read the msdn article,much more from other sites, about this function but I cant get it to work.

http://msdn.microsoft.com/en-us/library/ms686025(VS.85).aspx


can you help me by solving the next basic example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <conio.h>

main()
{
      int a=97,b,c;

      for(b=0;b<=2;b++) //3 lines
      {
      c=a+8; //# of letters each line
      for(;a<=c;a++) //printing them up
      putchar(a);
      putchar(10);
      }
      //BOOL WINAPI SetConsoleCursorPosition(  __in  HANDLE hConsoleOutput,  __in  COORD dwCursorPosition);
      //printf("\bo"); <-switching d with o
      //BOOL WINAPI SetConsoleCursorPosition(  __in  HANDLE hConsoleOutput,  __in  COORD dwCursorPosition);
      //printf("\bd"); <-switching o with d
      while(a);//a loop just to keep the window opened
}


I would like to switch the letter d with the letter o. what should I change\edit\delete\rewrite in the program?

extreme-thanks in advance!


Final solution(THANKS TO DUOAS!!!!):
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <conio.h>
#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;
  COORD                      result;
  if (!GetConsoleScreenBufferInfo(
         GetStdHandle( STD_OUTPUT_HANDLE ),
         &csbi
         ))
    return -1;
  return result.X;
  }

int wherey()
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  COORD                      result;
  if (!GetConsoleScreenBufferInfo(
         GetStdHandle( STD_OUTPUT_HANDLE ),
         &csbi
         ))
    return -1;
  return result.Y;
  }


main()
{
      int a=97,b,c;
      for(b=0;b<=2;b++) //3 lines
      {
      c=a+8; //# of letters each line
      for(;a<=c;a++) //printing them up
      putchar(a);
      putchar(10);
      }
      gotoxy(3,0); //replacing letters
      printf("o");
      gotoxy(5,1);
      printf("d");
      while(a);//a loop just to keep the window opened
      
}
Last edited on Sep 18, 2008 at 6:39pm
Sep 18, 2008 at 3:38pm
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
35
36
37
#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;
  COORD                      result;
  if (!GetConsoleScreenBufferInfo(
         GetStdHandle( STD_OUTPUT_HANDLE ),
         &csbi
         ))
    return -1;
  return result.X;
  }

int wherey()
  {
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  COORD                      result;
  if (!GetConsoleScreenBufferInfo(
         GetStdHandle( STD_OUTPUT_HANDLE ),
         &csbi
         ))
    return -1;
  return result.Y;
  }

Have fun!
[edit] Fixed a Pascal-ism [/edit]
Last edited on Sep 18, 2008 at 6:59pm
Sep 18, 2008 at 3:52pm
I'm a newbie at this, and I got no idea how to work with the code you gave me, sorry. Can you please explain?
Sep 18, 2008 at 4:43pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

...


int main()
  {
  gotoxy( 33, 9 );
  printf( "Hello world!" );

  gotoxy( 0, 22 );
  printf( "Press ENTER to quit" );
  while (getchar() != '\n');
  return 0;
  }
Sep 18, 2008 at 4:59pm
Alrighty! Thank you very much, my problem is now solved thanks to you! god bless ya man!
Sep 18, 2008 at 6:02pm
:-)
Sep 18, 2008 at 6:23pm
A small question to Duoas
Line 7 is coord.Y = row;, shouldn't it be coord.Y = line; like the function parameter?
Sep 18, 2008 at 6:36pm
sorry to interupt but i noticed that too, it isnt that hard to replace, so personally i had no problem with it.
but for others ur defently right, it is an essentially important point to clear up buddy
Sep 18, 2008 at 7:00pm
A small question to Duoas
Line 7 is coord.Y = row;, shouldn't it be coord.Y = line; like the function parameter?

Oops! Fixed. :-|

[edit] I just typed that in off the top of my (mostly awake) head, and I've been doing a lot of programming in Pascal lately, so my fingers don't always do what my head is thinking... Heh :-S
Last edited on Sep 18, 2008 at 7:03pm
Sep 18, 2008 at 8:25pm
lol cool
Topic archived. No new replies allowed.