Bug Fix - confirmation not processing newline
- Fixed bug where util.cpp:confirm was eating newlines, and not rewriting the prompt. Consequently, after confirm asked the question, and the user hit <Enter>, nothing was displayed but the newline. Now uses std::getline.
This commit is contained in:
23
src/util.cpp
23
src/util.cpp
@@ -41,17 +41,28 @@
|
|||||||
#include "../auto.h"
|
#include "../auto.h"
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Uses std::getline, because std::cin eats leading whitespace, and that means
|
||||||
|
// that if a newline is entered, std::cin eats it and never returns from the
|
||||||
|
// "std::cin >> answer;" line, but it does disply the newline. This way, with
|
||||||
|
// std::getline, the newline can be detected, and the prompt re-written.
|
||||||
bool confirm (const std::string& question)
|
bool confirm (const std::string& question)
|
||||||
{
|
{
|
||||||
std::cout << question << " (y/n) ";
|
|
||||||
std::string answer;
|
std::string answer;
|
||||||
std::cin >> answer;
|
|
||||||
|
|
||||||
answer = trim (answer);
|
do
|
||||||
if (answer == "y" || answer == "Y")
|
{
|
||||||
return true;
|
std::cout << question << " (y/n) ";
|
||||||
|
std::getline (std::cin, answer);
|
||||||
|
answer = lowerCase (trim (answer));
|
||||||
|
if (answer == "\n") std::cout << "newline\n";
|
||||||
|
}
|
||||||
|
while (answer != "y" &&
|
||||||
|
answer != "ye" &&
|
||||||
|
answer != "yes" &&
|
||||||
|
answer != "n" &&
|
||||||
|
answer != "no");
|
||||||
|
|
||||||
return false;
|
return (answer == "y" || answer == "ye" || answer == "yes") ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user