How can I set the cursor at the desired location on the console in C or C++?
I remember a function called gotoxy(x,y)
, but I think its deprecated. Is th
I figured out this to set the cursor.
#include
void setPos(std::ostream& _os, const std::streamsize& _x, const std::streamsize& _y)
{
char tmp = _os.fill();
if(_y>0) {
_os.fill('\n');
_os.width(_y);
_os << '\n';
}
if(_x>0) {
_os.fill(' ');
_os.width(_x);
_os << ' ';
}
_os.flush();
_os.fill(tmp);
}
int main(int argc, char **argv)
{
setPos(std::cout, 5, 5);
std::cout << "foo" << std::endl;
return 0;
}
To do more you'll need assumptions on the resolution or a lib like ncurses.