I'm trying my hand at C++ and I was wondering if there was any function in the standard libraries that would allow the program to move where the cursor (flashing thing that shows where text is input/output) appears on the screen.
For example, something like the locatexy(x,y) function in Turing.
If you know of such a function, or of a (free) library that has one, please set me in the right direction.
Archived topic from Iceteks, old topic ID:2232, old post ID:18801
Cursor Control in C++ (Console)
- Red Squirrel
- Posts: 29209
- Joined: Wed Dec 18, 2002 12:14 am
- Location: Northern Ontario
- Contact:
Cursor Control in C++ (Console)
I don't think there's any functions for that. The console is more part of windows and not the program itself, the program just runs in one. I might be wrong though...
Archived topic from Iceteks, old topic ID:2232, old post ID:18805
Archived topic from Iceteks, old topic ID:2232, old post ID:18805
Honk if you love Jesus, text if you want to meet Him!
Cursor Control in C++ (Console)
C++ is really designed to work with windows rather than just a simple text console. Within a window you can place text boxes wherever you like (each with its own name) and any text you send to each text box appears within it formatted to fit the size of that box (ie: automatically word-wraps etc). If you resize the box, the text will also word-wrap to the new size etc.
For printing simple text to the screen (ie: not using windows), you are limited to writing lines of text at a time and there isn't a specific cursor location function (ie: you can't just tell it to go to coordinates X, Y on the screen and then print). This is because C++ uses the same method of writing to the screen as it does to write to a file (you can't got to location X,Y in a file, but rather read/write lines of text)
You do get some control over where text appears along each line as it is written by using the printf function (or fprintf if writing to a file);
eg: printf( "Here are some numbers:
%f %.2f
", numb1, numb2 );
will look like;
Here are some numbers:
100.000000 200.00
If you want text to appear at specific locations you would be best to start playing about with dialog windows and placing text boxes on the dialog window in the places you want them. You then just simply specify which text box and the text to appear.
For example, I have a text box called IDC_TillerMax that appears on a dialog window. To send text to this text box I first format the text using the sprintf function and then display it in the text box using the SetDlgItemText() function;
eg;
sprintf(TextString,"%.0f",TillerMax);
SetDlgItemText(IDC_TillerMax,TextString);
The sprintf function creates a string called TextString that contains the value of the variable TillerMax formatted to zero decimal places. If you want 2 decimal places you would use %.2f instead etc.
The SetDlgItemText function then displays the TextString in the text box called IDC_TillerMax
Archived topic from Iceteks, old topic ID:2232, old post ID:18902
For printing simple text to the screen (ie: not using windows), you are limited to writing lines of text at a time and there isn't a specific cursor location function (ie: you can't just tell it to go to coordinates X, Y on the screen and then print). This is because C++ uses the same method of writing to the screen as it does to write to a file (you can't got to location X,Y in a file, but rather read/write lines of text)
You do get some control over where text appears along each line as it is written by using the printf function (or fprintf if writing to a file);
eg: printf( "Here are some numbers:
%f %.2f
", numb1, numb2 );
will look like;
Here are some numbers:
100.000000 200.00
If you want text to appear at specific locations you would be best to start playing about with dialog windows and placing text boxes on the dialog window in the places you want them. You then just simply specify which text box and the text to appear.
For example, I have a text box called IDC_TillerMax that appears on a dialog window. To send text to this text box I first format the text using the sprintf function and then display it in the text box using the SetDlgItemText() function;
eg;
sprintf(TextString,"%.0f",TillerMax);
SetDlgItemText(IDC_TillerMax,TextString);
The sprintf function creates a string called TextString that contains the value of the variable TillerMax formatted to zero decimal places. If you want 2 decimal places you would use %.2f instead etc.
The SetDlgItemText function then displays the TextString in the text box called IDC_TillerMax
Archived topic from Iceteks, old topic ID:2232, old post ID:18902
Cursor Control in C++ (Console)
If you're asking how to make the cursor go to a specific location, you can use the library:
<conio.h>
and use the _gotoxy(int, int);
function, it places the cursor to the desired location.
_gotxy(0,0); takes the cursor to the top left hand character of the screen.
There's also some functions in conio that find out the cursor's current location, but I'm not familiar with them.
Archived topic from Iceteks, old topic ID:2232, old post ID:19227
<conio.h>
and use the _gotoxy(int, int);
function, it places the cursor to the desired location.
_gotxy(0,0); takes the cursor to the top left hand character of the screen.
There's also some functions in conio that find out the cursor's current location, but I'm not familiar with them.
Archived topic from Iceteks, old topic ID:2232, old post ID:19227
- Red Squirrel
- Posts: 29209
- Joined: Wed Dec 18, 2002 12:14 am
- Location: Northern Ontario
- Contact:
Cursor Control in C++ (Console)
Yep, wrote a prank program that uses those functions fun stuff.
btw welcome to the forum.
Archived topic from Iceteks, old topic ID:2232, old post ID:19229
btw welcome to the forum.
Archived topic from Iceteks, old topic ID:2232, old post ID:19229
Honk if you love Jesus, text if you want to meet Him!
Cursor Control in C++ (Console)
#include <windows.h>
#include <stdio.h>
int main ( int argc, char** argv )
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {32, 11};
SetConsoleCursorPosition ( hConsole, pos );
printf ( "Hello World!
" );
}
return 0;
}
Archived topic from Iceteks, old topic ID:2232, old post ID:19259
#include <stdio.h>
int main ( int argc, char** argv )
{
HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
if ( INVALID_HANDLE_VALUE != hConsole )
{
COORD pos = {32, 11};
SetConsoleCursorPosition ( hConsole, pos );
printf ( "Hello World!
" );
}
return 0;
}
Archived topic from Iceteks, old topic ID:2232, old post ID:19259
Cursor Control in C++ (Console)
#include <windows.h>
#include <iostream>
using namespace std;
void gotoxy( short x, short y ) {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x, y };
SetConsoleCursorPosition( hStdout, position );
}
int main() {
cout << "top left" << endl;
cout << "next line";
gotoxy(0, 0);
cout << "top left again";
return 0;
}
Archived topic from Iceteks, old topic ID:2232, old post ID:20905
#include <iostream>
using namespace std;
void gotoxy( short x, short y ) {
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x, y };
SetConsoleCursorPosition( hStdout, position );
}
int main() {
cout << "top left" << endl;
cout << "next line";
gotoxy(0, 0);
cout << "top left again";
return 0;
}
Archived topic from Iceteks, old topic ID:2232, old post ID:20905