Use a Scanner to read user input from the terminal and gather basic data about the user.
cmake_minimum_required(VERSION 3.12)
project(Scanner_Demo C CXX ASM)
create_simple_executable(${PROJECT_NAME} Scanner_Demo.cpp)
static bool isAnswerNo (char const userInput[]);
char userInput[64];
char name[64];
unsigned int age;
pwOut <<
"Hello! I'd like to teach you how to use PropWare to read from the terminal!\n";
do {
pwOut <<
"First, what's your name?\n>>> ";
pwIn >> name;
pwOut <<
"And how old are you?\n>>> ";
pwIn >> age;
pwOut <<
"Is your name " << name <<
" and are you " << age <<
" years old?\n>>> ";
pwIn.
input_prompt(
"",
"Please enter either 'yes' or 'no' (y/n)\n>>> ", userInput,
sizeof(userInput),
} while (isAnswerNo(userInput));
pwOut <<
"Hello, " << name <<
"!\n";
return 0;
}
bool isAnswerNo (char const userInput[]) {
return 0 == strcmp("n", userInput) || 0 == strcmp("no", userInput);
}
void input_prompt(const char prompt[], const char failureResponse[], char userInput[], const size_t bufferLength, const Comparator< char > &comparator)
Prompt the user for input and store the value only if it is sanitized.