so i am new to c++ and my teacher told me to make a hotel management programme...
well to make it more cool looking i wanna add those arror keys scroll selection this " --> "
when press up key it goes to the first option, down to the other one and so on.
But i am not able to do it so if anyone could tell me how to do it and explain it by adding comments, it would be really appreciated. :P

thanks though

hi again, i checked out the code but thats not what i want (well could be used in future and i can use that in my odd and even game, thanks :P) lemme explain you again

example:-

here is a menu

--> hello

i want something like that, an arrow key for navigation

RE LAST POST

example:-
--> hello
peekaboo

now i press the down key

hello
-->peekaboo

press up key it goes to hello, like that

but thats not what i want

I'm aware of that. It's only a part of what you want, the part that recognizes arrow keys. You also need to move the cursor with something like gotoxy. My apologies, I thought it was obvious that I wasn't giving you a complete solution.

Actually, it's dead easy. Against my better judgment, since you haven't posted any code of your own, I'll give you a more complete sample.

#include <conio.h>
#include <iostream>
#include <string>
#include <vector>
#include <windows.h> 

using namespace std;

#define FOREGROUND_DEFAULT FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE
#define BACKGROUND_DEFAULT 0
#define BACKGROUND_HIGHLIGHT BACKGROUND_BLUE | BACKGROUND_INTENSITY

/* System dependent key codes */
enum
{
    KEY_ESC     = 27,
    ARROW_UP    = 256 + 72,
    ARROW_DOWN  = 256 + 80,
    ARROW_LEFT  = 256 + 75,
    ARROW_RIGHT = 256 + 77
};

static int get_code()
{
    int ch = getch();

    if (ch == 0 || ch == 224)
    {
        ch = 256 + getch();
    }

    return ch;
}

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

void show_option(const string& option, bool highlighted)
{
    if (highlighted)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_DEFAULT | BACKGROUND_HIGHLIGHT);
    }
    else
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_DEFAULT | BACKGROUND_DEFAULT);
    }

    cout << option << '\n';
}

int main()
{
    vector<string> options;
    int ch, pos = 0;

    options.push_back("1) Option 1");
    options.push_back("2) Option 2");
    options.push_back("3) Option 3");
    options.push_back("4) Option 4");

    for (int i = 0; i < options.size(); i++)
    {
        show_option(options[i], i == pos);
    }

    gotoxy(pos, 0);

    while ((ch = get_code() ) != KEY_ESC)
    {
        switch (ch) {
        case ARROW_UP:
            if (pos > 0)
            {
                show_option(options[pos], false);
                gotoxy(0, --pos);
                show_option(options[pos], true);
                gotoxy(0, pos);
            }

            break;
        case ARROW_DOWN:
            if (pos < options.size() - 1)
            {
                show_option(options[pos], false);
                gotoxy(0, ++pos);
                show_option(options[pos], true);
                gotoxy(0, pos);
            }

            break;
        }
    }

    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_DEFAULT | BACKGROUND_DEFAULT);
}

@Gensk: You didn't say what operating system you are uing. The sugestion by deceptkion works only on MS-Windows. If you are using *nix then you will have to do something else.

This article has been dead for over six months. Start a new discussion instead.