0

I am wondering how to make a multiple choice prompt in the console where you can select the options with the arrow keys. So instead of something like this:

#include <iostream>
#include <string>

int main()
{
    std::string selection;
    std::cout << "A. option 1";
    std::cout << "B. option 2";
    std::cin >> selection;

    if(selection == "A") {
        //do whatever;
    } else if(selection == "B") {
        //do something else;
    } else {
        //repeat the prompt
    }
return 0;
}

I could have something that looks like this, but without the fancy UI: image

SuperWill
  • 9
  • 5
yzzyx
  • 39
  • 3
  • 3
    Well, I don't think you can with `cin` or anything standard for the matter. This "fancy UI" seems to be ncurses; you could use it for your program. – asu Jul 18 '18 at 19:56
  • Yep, from the image it is [ncurses](https://www.gnu.org/software/ncurses/). you don't want to implement such stuff portably based on `iostream` facilities on your own. – πάντα ῥεῖ Jul 18 '18 at 20:13

1 Answers1

3

This is by no means an all encompassing answer, but i'm hoping it will help with the basic idea.

I wrote up a quick C++ program which preforms the logical part of a multiple choice picker using only the up arrow, down arrow, and enter key.

#include <iostream>
#include <conio.h>//For _getch().

//https://stackoverflow.com/questions/24708700/c-detect-when-user-presses-arrow-key
#define KEY_UP 72       //Up arrow character
#define KEY_DOWN 80    //Down arrow character
#define KEY_ENTER '\r'//Enter key charatcer

int main(){
    int selected = 0;    //Keeps track of which option is selected.
    int numChoices = 2; //The number of choices we have.
    bool selecting = true;//True if we are still waiting for the user to press enter.
    bool updated = false;//True if the selected value has just been updated.

    //Output options
    std::cout << "A. Option 1\n"; 
    std::cout << "B. Option 2\n";

    char c; //Store c outside of loop for efficiency.
    while (selecting) { //As long as we are selecting...
        switch ((c = _getch())) { //Check value of the last inputed character.
            case KEY_UP:
                if (selected > 0) { //Dont decrement if we are at the first option.
                    --selected;
                    updated = true;
                }
                break;
            case KEY_DOWN:
                if (selected < numChoices - 1) { //Dont increment if we are at the last option.
                    ++selected;
                    updated = true;
                }
                break;
            case KEY_ENTER:
                //We are done selecting the option.
                selecting = false;
                break;
            default: break;
        }
        if (updated) { //Lets us know what the currently selected value is.
            std::cout << "Option " << (selected + 1) << " is selected.\n";
            updated = false;
        }
    }
    //Lets us know what we ended up selecting.
    std::cout << "Selected " << (selected + 1) << '\n';
    return 0;
}

I used this stack overflow answer to determine how to track key presses in the console. This answer may also prove useful for moving the console cursor around when changing the background color of text.

Good luck!

Zackary Jones
  • 371
  • 1
  • 9