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:
Paul Beckingham 2009-03-20 16:56:25 -04:00
parent cc2220b406
commit 74ea5b4ef6

View file

@ -41,17 +41,28 @@
#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)
{
std::cout << question << " (y/n) ";
std::string answer;
std::cin >> answer;
answer = trim (answer);
if (answer == "y" || answer == "Y")
return true;
do
{
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;
}
////////////////////////////////////////////////////////////////////////////////