# \[C++\] I'm Stuck: How to directly react on the pressed Tab Key?

**URL:** <https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [January 27, 2025, 4:57pm UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504 "2025-01-27T16:57:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Clostosus](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/clostosus/32/4601_2.png) [@Clostosus](https://forum.codecrafters.io/u/Clostosus)\
**Post date:** [January 27, 2025, 4:57pm UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504/1 "2025-01-27T16:57:22Z")

</div>

I’m stuck on Stage #(QP2).

I’ve previously read all my commands as one line with getline().  
I now use getchar(), but pressing the Tab-Key still does not lead to any direct result. It leads to a result, but I first need to tap enter(\n) after pressing it.

Here is the relevant function in my C++ code:

```c++

void CommandReader::readCharacterByCharacter(std::string &currentInput, CommandManager &manager) {
    char c;
    while (true) {
        c = static_cast<char>(getchar());
        if (c == '\n') {
            return; // Input line completed
        } else if (c == '\t') {
            // Autocompletion for Tab Key
            std::vector<std::string> * suggestions = manager.getAllNamesWithPrefix(currentInput);
            if (suggestions && suggestions->size() == 1) {
                 currentInput = suggestions->front();
                 std::cout << "\r" << currentInput << std::flush;  
            }
            delete suggestions;
        } else { 
            currentInput += c;
        }
    }
}

```

Here is a link to my GitHub [Repo](https://github.com/Clostosus/codecrafters-shell-cpp).

---

<div class="post-metadata">

**Author:** ![maximtereshchenko](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/maximtereshchenko/32/3516_2.png) [@maximtereshchenko](https://forum.codecrafters.io/u/maximtereshchenko)\
**Post date:** [February 1, 2025, 11:59am UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504/3 "2025-02-01T11:59:26Z")

</div>

Hi! This is because you most probably run your shell through another shell (e.g. `bash`). To disable the input buffering feature in `bash`, you can:

- Enable raw mode by running `stty -icanon -echo` (disable input buffering and automatic echoing what you type in) before running your shell. You can restore the defaults by `stty sane` after your shell exits.
- Other option, which seemed to me more safe, is to read the input one by one to pass in to your shell. Here is my approach: [codecrafters-shell-java/.codecrafters/run.sh at master · maximtereshchenko/codecrafters-shell-java · GitHub](https://github.com/maximtereshchenko/codecrafters-shell-java/blob/master/.codecrafters/run.sh#L5)

---

<div class="post-metadata">

**Author:** ![Clostosus](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/clostosus/32/4601_2.png) [@Clostosus](https://forum.codecrafters.io/u/Clostosus)\
**Post date:** [February 1, 2025, 7:50pm UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504/4 "2025-02-01T19:50:47Z")

</div>

Thank you maxim. 😊  
I otherwise would have searched a month for the bug in my code that was never in my code.  
You seem to be using lots of design patterns. Any refactoring ideas for my shell-project?  
Click Me

---

<div class="post-metadata">

**Author:** ![Clostosus](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/clostosus/32/4601_2.png) [@Clostosus](https://forum.codecrafters.io/u/Clostosus)\
**Post date:** [February 1, 2025, 7:53pm UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504/5 "2025-02-01T19:53:06Z")

</div>

Thank you maxim. 😊  
Do you have any other advice or a refactoring idea for my project?

 ![Success](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/8/852941b4e870aa730c76843d79e02ae670374d11.png)

---

<div class="post-metadata">

**Author:** ![maximtereshchenko](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/maximtereshchenko/32/3516_2.png) [@maximtereshchenko](https://forum.codecrafters.io/u/maximtereshchenko)\
**Post date:** [February 2, 2025, 8:29am UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504/6 "2025-02-02T08:29:34Z")

</div>

My biggest breakthough was when I realized that the “frontend” should be separated from the “backend” for this project (as always for any other project). More precisely, the logic related to printing to standard out and standard error should be separated from the core shell logic. On a side note: `test driven development` helped me to refactor the project multiply times while guideing the development along the way, highly recommend to learn this skill. Good luck on your endeavors!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/3X/7/0/700657133935c15703e22c7c8870394f6e6dc27d.svg) [@system](https://forum.codecrafters.io/u/system)\
**Post date:** [February 7, 2025, 8:29am UTC](https://forum.codecrafters.io/t/c-im-stuck-how-to-directly-react-on-the-pressed-tab-key/4504/7 "2025-02-07T08:29:55Z")

</div>

Closed on February 7, 2025, 8:29am UTC

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.
