Thread: Console Cursor Position

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    45

    Console Cursor Position

    I am using the gotoxy function and I have a question:
    Is it possible to get the current x and y coordinates of your program f.e first gotoxy to lets say 5,6 and then return to the spot the cursor came from

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    23
    If you have access to Win32 APIs, GetConsoleScreenBufferInfo can help you.

    I don't think there is a portable way to get console cursor position anyway... Anyone else?
    Merc

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    Well i came this far but it returns only crap, because I dont know how to use GetConsoleScreenBufferInfo properly I just guessed somewhat



    Code:
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    	PCONSOLE_SCREEN_BUFFER_INFO pcsbi=&csbi;
        HANDLE h;
    	
    	GetConsoleScreenBufferInfo(h,pcsbi);
        
    	cout<<csbi.dwCursorPosition.X<<","<<csbi.dwCursorPosition.Y;

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    You must be having functions if it is console..

    wherey() and wherex() this is found in the header conio.h but remember this is not standard.. so you have a chance that this is not present in your compiler...

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    no I couldnt find t in my compiler......
    somebody else has an solution?

  6. #6
    0x01
    Join Date
    Sep 2001
    Posts
    88
    Over here: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Code:
    #include <windows.h> 
    
    void gotoxy(int x, int y)
    {
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    Last edited by knave; 04-25-2003 at 07:33 AM.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    Code:
    void t1 ( void ) 
    {    CONSOLE_SCREEN_BUFFER_INFO csbi;
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);      
    
        COORD   pos = { 10, 12 };
        SetConsoleCursorPosition( h, pos );
        WriteConsole( h, "hello\nworld", 11, NULL, NULL );
        // does NOT change cursor position    // cout << "foo";
     
        GetConsoleScreenBufferInfo(h,&csbi);
        cout<<csbi.dwCursorPosition.X<<","<<csbi.dwCursorPosition.Y<<endl;}
    where are these good for?

    and if I try this:
    Code:
    CONSOLE_SCREEN_BUFFER_INFO csbi;    
    	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
                    //cout<<"foo";
    	cout<<"\n\n\n"<<"12345";
    	GetConsoleScreenBufferInfo(h,&csbi);
        cout<<csbi.dwCursorPosition.X<<","<<csbi.dwCursorPosition.Y<<endl;
    I want it to have it display: "3,5" wich it doesnt
    It seems display only the Y-value if I use endl

    Is there a way to have -cout<<"foo"- change the cursor position
    so that other "cout's" change the cursor position as well
    Last edited by Ivan!; 04-25-2003 at 10:00 AM.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    45
    As I dont know where cout.flush() is good for, I dont know why it works, but who cares: it works
    Thanks!

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    It flushes everything out of the stream. Basically, if anything is in the stream, it is immediately written to the proper location upon the calling of flush.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 09:59 PM
  2. Full Screen Console
    By St0rmTroop3er in forum C++ Programming
    Replies: 1
    Last Post: 09-26-2005, 09:59 PM
  3. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM
  4. Console Functions Help
    By Artist_of_dream in forum C++ Programming
    Replies: 9
    Last Post: 12-04-2004, 03:44 AM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM