# (C++) "Build your own shell" - Stage #MG5: Expected prompt ("$ ") to be printed, got ""

**URL:** <https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [December 9, 2024, 11:43am UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373 "2024-12-09T11:43:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![antoninoLorenzo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/antoninolorenzo/32/3404_2.png) [@antoninoLorenzo](https://forum.codecrafters.io/u/antoninoLorenzo)\
**Post date:** [December 9, 2024, 11:43am UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/1 "2024-12-09T11:43:48Z")

</div>

I’m stuck on Stage #MG5 in “Build your own shell”, where the task is to find an executable in PATH.

The error log is:

```auto
remote: [tester::#MG5] Running tests for Stage #MG5 (The type builtin: executable files)
remote: [tester::#MG5] Running ./your_program.sh
remote: [tester::#MG5] Expected prompt ("$ ") to be printed, got ""
remote: [tester::#MG5] Test failed

```

Here is the code:

```c++
// ...

int main() {
    // parse PATH
    const char* path_raw = getenv("PATH");
    if (path_raw != nullptr) {
        std::string path_str = path_raw;
        split(&PATH, path_str, PATH_DELIMITER);
    }

    // map command names to functions
    BUILTIN_FUNCTIONS["echo"] = [](std::string input){
        __builtin_echo(input);
    };
    BUILTIN_FUNCTIONS["type"] = [](std::string input){
        __builtin_type(input);
    };

    // Flush after every std::cout / std:cerr
    // Note: I tried removing also this
    std::cout << std::unitbuf;
    std::cerr << std::unitbuf;

    // REPL
    while (true) {
        std::cout << "$ "; // here is the $...

        std::string input, command, command_args;
        std::getline(std::cin, input);
        if (input.compare(0, 4, "exit") == 0)
            break;
        
        command = input.substr(0, input.find(" "));
        command_args = input.substr(input.find(" ")+1); 
        
        if (BUILTIN_FUNCTIONS.count(command))
            BUILTIN_FUNCTIONS[command](command_args);
        else
            std::cout << input << ": command not found" << std::endl;
    }
}

```

> Note: I tried setting `debug` to true in `codecrafters.yml`, the log is still the same.

The reason isn’t clear, since it previously worked. I tried reverting some changes I made in the code (for example using macros to define the delimiter “;” or “:” based on OS), however it still doesn’t work.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 9, 2024, 11:45am UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/2 "2024-12-09T11:45:40Z")

</div>

Hi @antoninoLorenzo, could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

---

<div class="post-metadata">

**Author:** ![antoninoLorenzo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/antoninolorenzo/32/3404_2.png) [@antoninoLorenzo](https://forum.codecrafters.io/u/antoninoLorenzo)\
**Post date:** [December 9, 2024, 11:53am UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/3 "2024-12-09T11:53:11Z")

</div>

Here is the link [GitHub - antoninoLorenzo/antoninoLorenzo-codecrafters-shell-cpp](https://github.com/antoninoLorenzo/antoninoLorenzo-codecrafters-shell-cpp.git)

_I’m new to C++ please be kind 😅_

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 9, 2024, 11:53am UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/4 "2024-12-09T11:53:56Z")

</div>

Thanks for sharing the link! I’ll take a look and get back to you by the end of the week.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 15, 2024, 2:57pm UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/6 "2024-12-15T14:57:20Z")

</div>

I tried running your code against the previous stages, but it’s actually no longer passing the first stage **#OO8 (Print a prompt)**.

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/d/def18fc5f7448db7005ed697dfbecf20c3e18677.png)

Suggestions:

1. Use [our CLI](https://docs.codecrafters.io/cli/installation) to test against previous stages by running:

```auto
codecrafters test --previous

```

1. Focus on fixing the early stages first, as later stages depend on them.

---

<div class="post-metadata">

**Author:** ![antoninoLorenzo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/antoninolorenzo/32/3404_2.png) [@antoninoLorenzo](https://forum.codecrafters.io/u/antoninoLorenzo)\
**Post date:** [December 15, 2024, 4:27pm UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/8 "2024-12-15T16:27:11Z")

</div>

Running `codecrafters test --previous` gaves me the same result (failed).

To debug it I commented out the first section of the code where I initialized the `PATH` vector and `BUILTIN_FUNCTIONS` map. After some trials I found that the issue is caused by the `split` function.

Here is the interested section of the code:

```auto
#if __linux__
    const char *PATH_DELIMITER = ":";
    const char *PATH_SEPARATOR = "/";
#elif _WIN32
    const char *PATH_DELIMITER = ";";
    const char *PATH_SEPARATOR = "\\";
#else
    const char *PATH_DELIMITER = ":";
    const char *PATH_SEPARATOR = "/";
#endif

// define PATH at compile time, but populate at program start.
// Note: std::vector is a dynamic array
std::vector<std::string> PATH;

// ...

void split(
    std::vector<std::string> *vec_ptr,
    std::string str,
    const char *delimiter
){
    int start = 0, end;
    while (start < str.size()){
        end = str.find(delimiter, start+1) + 1;
        vec_ptr->push_back(str.substr(start, end - start - 1));
        start = end;
    }
}

```

If I do the following I do not get any errors:

```c++
  size_t start = 0, end;
  while (start < str.size()){
      end = str.size();
      // end = str.find(delimiter, start+1) + 1;
      // vec_ptr->push_back(str.substr(start, end - start - 1));
      start = end;
  }

```

The moment I use `end = str.find(...)` I get the error.

Do you have any idea why this happen?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [December 15, 2024, 8:30pm UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/9 "2024-12-15T20:30:15Z")

</div>

To be honest, I’m not entirely sure, as my C++ is a bit rusty. However, I believe you can try `std::getline`, rather than rolling your own split function:

```cpp
        std::stringstream ss(path_str);
        std::string segment;
        while (std::getline(ss, segment, PATH_DELIMITER)) {
            PATH.push_back(segment);
        }

```

---

<div class="post-metadata">

**Author:** ![antoninoLorenzo](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/antoninolorenzo/32/3404_2.png) [@antoninoLorenzo](https://forum.codecrafters.io/u/antoninoLorenzo)\
**Post date:** [December 16, 2024, 12:32pm UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/10 "2024-12-16T12:32:09Z")

</div>

Thanks, it now works with the previous tests. Kinda sad for the algorithm with start and end counter, I really liked it.

---

<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:** [December 21, 2024, 12:32pm UTC](https://forum.codecrafters.io/t/c-build-your-own-shell-stage-mg5-expected-prompt-to-be-printed-got/3373/11 "2024-12-21T12:32:32Z")

</div>

Closed on December 21, 2024, 12:32pm UTC

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