How to show a list of options and allow the user to select the options by using arrow keys and enter.

>How to show a list of options and allow the user to select the options by using arrow keys and enter.
How to include your operating system and compiler in the question because manipulating the arrow keys is platform-dependent.

You might do something like this:

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

using namespace std;

enum
{
  KEY_ESC     = 27,
  ARROW_UP    = 256 + 72,
  ARROW_DOWN  = 256 + 80
};

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

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

  return ch;
}

void menu(int current)
{
  static const string options[] = {
    "Option 1",
    "Option 2",
    "Option 3",
    "Option 4",
  };

  for (int i = 0; i < 4; i++)
    cout<< (i == current ? "> " : "  ") << options[i] <<endl;
}

int main()
{
  bool done = false;
  int current = 0;

  while (!done) {
    system("cls");
    menu(current);
    switch (get_code()) {
    case ARROW_UP:
      current = current == 0 ? 3 : current - 1;
      break;
    case ARROW_DOWN:
      current = current == 3 ? 0 : current + 1;
      break;
    case KEY_ESC:
      done = true;
      break;
    case '\r':
      cout<<"Executing option"<<endl;
      done = true;
    }
  }
  cin.get();
}

>finally a good use for system("cls");
A use, yes. A good use, no. I do recall saying that one of the valid situations for clearing the screen is when you control the window and intend to do animation or silly menu hierarchies. However, they aren't that valid because animation and menu hierarchies should be done through a graphical interface, not the command line. However, I'm too lazy to write up a graphical example, so the OP can just deal with it.

>and you did it without the stdlib.h
Indeed. That tends to happen when I type too quickly.

note: <conio> isnt ANSI c++, but DevC++ does support it! You can also process messages if you want to write a windows app. Start a new DevC++ win32 project and add some code like this in the windowprocedue switch statement

case WM_KEYDOWN:
{ 
    int VirtKey = (int) wParam;    // virtual-key code 
    int lKeyData = lParam;          // key data 
}
break;

>note: <conio> isnt ANSI c++
I'm sure that everyone at Daniweb is fully aware of that by now. :rolleyes:

current = current == 0 ? 3 : current - 1;

I cant understand this line. Also is it possible to create a background colour for the option if it is selected?

current = current == 0 ? 3 : current - 1;

this is shorthand for

if(current == 0)
    current = 3;
else
    current--; // current = current - 1

in general

(x == 0) ? a : b;

if the bracketed logic (x == 0) is true then x = a
if the bracketed logic (x == 0) is false then x = b

you dont even need the x = bit at the beginning!

>I cant understand this line.
If you hit the up arrow key and the current selection is the first item, it causes the selection to roll over to the last item.

>Also is it possible to create a background colour for the option if it is selected?
Yes, it is. Search MSDN for SetConsoleTextAttribute.

This question has already been answered. Start a new discussion instead.