Skip to content
aelliixx edited this page Mar 20, 2024 · 3 revisions

Welcome to the cpp-inquirer wiki! I hope to provide some usage examples even the most beginner users can follow.

Examples

The simplest example

std::string answer = alx::Question{"any key", "What's your name?"}.ask(); // Keys do not matter in this example

Or you can instantiate a Question object and retrieve the answer later.

alx::Question question{"any key", "What's your name?"}.ask();
std::string answer = question.get_answer();

Using the Inquirer class

auto inquirer = alx::Inquirer();
inquirer.add_question({ "query", "What do you want to do?" }); // Keys *do* matter when using them with 'alx::Inquirer' class
inquirer.ask();

Asking the question right away

auto inquirer = alx::Inquirer();
inquirer.add_question({ "query", "What do you want to do?" }).ask();
// Calling inquirer.ask() would not prompt the user with an answered question again. To ask the user again, call 'ask()' like this
inquirer.ask(true);

Retrieving answers from Inquirer class

std::string answer = inquirer.answer("query"); // Where "query" is the key of your question.

Asking the question until a criteria is met

std::string name;
alx::Question question{ "query", "What's your name?" };
while (name.empty()) // This is your criteria
    name = question.ask(true);