How can I use the function gotoxy in Dev C++ 4? I need use it to put a text string in the center of the screen. I used to do that in Turbo C++ 3.0 by writting in the preprocessor section the following instruction: #include <conio.h>, but when I used it in Dev C++ 4 it didn't work! Please help me!
Postdata: I'm sorry... I have a bad english...
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
look up the FAQ and a decent C++ tutorial: "gotoxy" is NOT standard, it's Borland's own stuff, you'll want to deal with STANDARD features that are USABLE with any compiler.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2005-06-30
Use the Win32 console functions. They are not standard either, but will work on any Win32 compiler so are somewhat more portable.
I think people get carried away with standards some times. Often there's a job to and you have to write something platform specific. Here's a try at gotoxy for Win32 console
include <iostream>
include <windows.h>
using namespace std;
void gotoxy( int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2005-06-30
I agree - and nice example code also!.
The point of the standard library is not to restrict yourself to it, but to guarantee a minimum set of functions that you can reasonably expect to be available on all platforms.
It is important to be aware of the standard library and what is in it (and what is not), so that when you are writing code you can consider the consequences of using propriatary or third party interfaces.
The OP was surprised that gotoxy() was not available, because presumambly when he originally used it in Turbo C/C++, he was not aware that it could not reasonable be expected ot be avaiable in other compilers.
The comment "you'll want to deal with STANDARD features that are USABLE with any compiler[sic]." is unreasonable (and unnecessarily capitalised!). There is no standard or cross-platform way to do what the OP wants. Also it is not unreasonable to make direct calls to the operating system (although there are ways of avoiding it, but they use 3rd party non-standard libraries!).
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for the code. I made a small modification to your gotoxy. I made it so that it can be used like:
cout(x, y) << "output here";
sample code follows:
include <iostream>
include <iosfwd>
namespace my {
void locate(int x, int y );
std::ostream& cout( int x = -1, int y = -1 ) {
if(~x || ~y){
locate(x,y);
}
return std::cout;
}
void cls();
void wait(){std::cin.get();}
}
using namespace my;
int main(){
cout() << "my::cout() behaves just like std::cout\n";
cout() << "with letters following the cursor";
cout(20,12) << "but you can use the function with arguments";
cout(20,13) << "to print to arbitrary coordinates";
wait();
cls();
cout(0,0) << "All";
wait();
cout(76, 0) << "you";
wait();
cout(75, 23) << "need";
wait();
cout(0,23) << "is";
wait();
cout(38, 12) << "love";
wait();
cls();
cout(25, 12) << "goodbye cruel world";
cout(0,24) << "Press Enter to continue";
std::cin.get();
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2005-07-04
Neat.
It may have been more in keeping with iostream conventions however to have created a new io manipulator so that the code would look like:
cout << curpos( x, y ) << "output here" ;
I thought of that, then I thought "how easy is that?". I found this article http://cpptips.hyperformix.com/cpptips/manipulatorI. The author does not go into detail about manipulators with more than one parameter (and suggests avoiding them!). The site is not well linked, but replace the I at teh end of teh URL with II and III respectively to get the full article. Even the highr level linking page (when I found it) only links to I and II. Once found however, I discovered a huge archive of cool C++ related stuff! Albeit somewhat hard to navigate start at http://cpptips.hyperformix.com/cpptips.html for teh tips, the root (http://cpptips.hyperformix.com) has a huge set of C++ and general programming links.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
How can I use the function gotoxy in Dev C++ 4? I need use it to put a text string in the center of the screen. I used to do that in Turbo C++ 3.0 by writting in the preprocessor section the following instruction: #include <conio.h>, but when I used it in Dev C++ 4 it didn't work! Please help me!
Postdata: I'm sorry... I have a bad english...
I agree that this:
cout << curpos( x, y ) << "output here" ;
is the more c++ way. However, I thought that:
cout(x,y) << "output here";
is easier to use/read in the code.
Anyway...I just thought that this might be helpful to someone, or that someone might offer a better solution. Cheers. Slipnot
look up the FAQ and a decent C++ tutorial: "gotoxy" is NOT standard, it's Borland's own stuff, you'll want to deal with STANDARD features that are USABLE with any compiler.
Use the Win32 console functions. They are not standard either, but will work on any Win32 compiler so are somewhat more portable.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/console_functions.asp
SetConsoleCursorPosition() is specifically what you want. But be aware Win32 console windows can be any size, and can change size.
There is no cross-platform method of console manipulation beyond simple 'glass teletype' style output.
Clifford
I think people get carried away with standards some times. Often there's a job to and you have to write something platform specific. Here's a try at gotoxy for Win32 console
include <iostream>
include <windows.h>
using namespace std;
void gotoxy( int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}
int main(int argc, char * argv[] )
{
gotoxy( 6, 6 );
cout << "Line 1";
gotoxy( 2, 2 );
cout << "Line 2";
gotoxy( 0, 0 );
cout << "Line 3";
}
I agree - and nice example code also!.
The point of the standard library is not to restrict yourself to it, but to guarantee a minimum set of functions that you can reasonably expect to be available on all platforms.
It is important to be aware of the standard library and what is in it (and what is not), so that when you are writing code you can consider the consequences of using propriatary or third party interfaces.
The OP was surprised that gotoxy() was not available, because presumambly when he originally used it in Turbo C/C++, he was not aware that it could not reasonable be expected ot be avaiable in other compilers.
The comment "you'll want to deal with STANDARD features that are USABLE with any compiler[sic]." is unreasonable (and unnecessarily capitalised!). There is no standard or cross-platform way to do what the OP wants. Also it is not unreasonable to make direct calls to the operating system (although there are ways of avoiding it, but they use 3rd party non-standard libraries!).
Clifford
Thanks for the code. I made a small modification to your gotoxy. I made it so that it can be used like:
cout(x, y) << "output here";
sample code follows:
include <iostream>
include <iosfwd>
namespace my {
void locate(int x, int y );
std::ostream& cout( int x = -1, int y = -1 ) {
if(~x || ~y){
locate(x,y);
}
return std::cout;
}
void cls();
void wait(){std::cin.get();}
}
using namespace my;
int main(){
cout() << "my::cout() behaves just like std::cout\n";
cout() << "with letters following the cursor";
cout(20,12) << "but you can use the function with arguments";
cout(20,13) << "to print to arbitrary coordinates";
wait();
cls();
cout(0,0) << "All";
wait();
cout(76, 0) << "you";
wait();
cout(75, 23) << "need";
wait();
cout(0,23) << "is";
wait();
cout(38, 12) << "love";
wait();
cls();
cout(25, 12) << "goodbye cruel world";
cout(0,24) << "Press Enter to continue";
std::cin.get();
int t=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
cls();
}
t=clock()-t;
cout()<< t << "secs";
cls();
int t2=clock();
for(int z=0; z<100; z++){
cout(z%80, z%25) << z;
system("cls");
}
t2 = clock() - t2;
cls();
cout()<< t << std::endl << t2 ;
wait();
return 0;
}
include <windows.h>
void my::locate(int x, int y )
{
COORD cur = { x, y };
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), cur );
}
include <string>
void my::cls(){
static const std::string b(2000, ' ');
my::cout(0,0) << b;
my::locate(0,0);
}
----end code---
btw...I got help on comp.lang.c++
Neat.
It may have been more in keeping with iostream conventions however to have created a new io manipulator so that the code would look like:
cout << curpos( x, y ) << "output here" ;
I thought of that, then I thought "how easy is that?". I found this article http://cpptips.hyperformix.com/cpptips/manipulatorI. The author does not go into detail about manipulators with more than one parameter (and suggests avoiding them!). The site is not well linked, but replace the I at teh end of teh URL with II and III respectively to get the full article. Even the highr level linking page (when I found it) only links to I and II. Once found however, I discovered a huge archive of cool C++ related stuff! Albeit somewhat hard to navigate start at http://cpptips.hyperformix.com/cpptips.html for teh tips, the root (http://cpptips.hyperformix.com) has a huge set of C++ and general programming links.
Clifford