1

I'm sorry for the weird title but i really don't know how to say this. Look:

std::array<std::string, 2> names = { "chloe", "tom" };
    std::string choice;

    std::cout << "Type a name.";
    std::getline(std::cin, choice);

    std::for_each(choice.begin(), choice.end(), [](char& c) {
        c = ::tolower(c);
        });

    for (int i = 0; i < names.size(); i++) {
        if (choice == names[i]) {
            std::cout << choice;
        }
    }

This code lets the user input a name and if the name is inside the names array it gets printed. What i want to do is that as soon as the user types a correct name (chloe for example), even without having to press enter the program recognizes it as correct and proceeds.

akaManen
  • 27
  • 6
  • what if the array contains "chloe" and "chloephelia"? – 463035818_is_not_an_ai Apr 29 '21 at 16:57
  • @largest_prime_is_463035818 chloe would get checked first and then the user will be able to type chloephelia, in my original program when a name is inserted it wont be recognized again. – akaManen Apr 29 '21 at 16:59
  • By default, the terminal does preprocessing of raw input, and gives your app the final processed input. If you want to handle things "as they type" then you have to handle the raw inputs, including left arrow to insert, and backspace :( – Mooing Duck Apr 29 '21 at 17:33
  • akaManen, With "let the user insert stuff without pressing enter", what would you expect if input included a _backspace_ `'\b'`? Act like any other character or backspace over the prior character or what? – chux - Reinstate Monica Apr 29 '21 at 17:37
  • Both the C and C++ Standard libraries have to support a very wide umbrella of systems, and a staggering amount of hardware. As a result, it supports the lowest common denominator for pretty much everything in order to cover everyone. That means there a lot of nice features I'd love to have that just aren't there. For example, guarantees of 2s compliment arithmetic finally got added to the C++ Standard last year after spending decades as the de facto standard. I guess 3's compliment failed or something. Probably wasn't much point after John Ritter passed on. – user4581301 Apr 29 '21 at 17:44
  • @chux-ReinstateMonica i didnt think about backspace, but if there is a way for the program to check the input that is remaining after every action it would be all right, for example user types "a", program checks and a is not inside the array so it does nothing, after some time when the input is "ales" the user presses backspace and the program should just go back and check "ale" again, obv it's not going to proceed because "ale" isn't right but that would let the user correct himself. – akaManen Apr 30 '21 at 05:32

0 Answers0