Cursor Position

I'm making a hangman game in console c++.
I need to change the cursor's position and would also like to
change the color of certain outputs - not everything.

I want something that would work cross-platform, but if that doesn't exist or if that is much more complex than a windows-only way, then so be it.

I'm kind of new to c++ so I don't want anything too complex if that's possible.
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
#ifdef _WIN32

  #include <windows.h>

  void gotoxy( int x, int y )
    {
    COORD p = { x, y };
    SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), p );
    }

#else

  #include <unistd.h>
  #include <term.h>

  void gotoxy( int x, int y )
    {
    int err;
    if (!cur_term)
      if (setupterm( NULL, STDOUT_FILENO, &err ) == ERR)
        return;
    putp( tparm( tigetstr( "cup" ), y, x, 0, 0, 0, 0, 0, 0, 0 ) );
    }

#endif 

Make sure to link properly with the Unix version. You'll need to link with one of
-lcurses
-lterminfo
-lncurses
(whichever is appropriate for your system).

Good luck!
How to you mean link properly?
It's ubuntu (the latest one)

The code worked in windows but not ont he ubuntu.
And isn't there a better way?
Your way is like using

#ifdef _WIN32
system("CLS");

#else
system("clear");

#endif

Doesn't seem like a good way
Well sorry to say this but...

http://www.cplusplus.com/forum/articles/28558/

The console is realy just for displaying information, I tried to make a hangman game in the console, and got an ugly thing working after a while, then I made an rpg with a map!

Ugh... When I moved, in the rpg game, the text flickered like mad...

What I am trying to say is, learn graphics, and It will be a Lot easier and more fun!

P.s. http://lazyfoo.net/SDL_tutorials/lesson01/index.php Best tutorial ever :D
@torchi12
Yes, my way is like that, but doesn't do anything evil.
You want to do something OS-specific, so you cannot complain that your code must be different for different OSes.

On Ubuntu, link with
g++ your stuff here -lcurses
and the code will run properly. (You must have the ncurses development library installed, BTW.)

I just noticed that I didn't give you color information. This is a bit more difficult than other things... Can you get along without it? (If not, I'll help you out, but remember that it isn't easy.)

You might want to consider using the NCurses library directly... Then your code will be nearly identical for both Windows and *nix...

@Rtme
Oh no! Too bad I didn't know that!

All the console hatred Disch puts out is just opinion, however well-informed.
Many good games have been written for use on console systems, and some still are.

Programming the console is notoriously difficult, and I dare say that the learning curve is a little higher than nice object systems like SDL and SFML. But that doesn't mean that elementary programming exercises like a simple hangman game are not worth one's time, and they are significantly easier than having to deal with either of the other systems at this stage.
Topic archived. No new replies allowed.