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

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:

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:

// ...

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.

Hi @antoninoLorenzo, could you upload your code to GitHub and share the link? It will be much easier to debug if I can run it directly.

Here is the link GitHub - antoninoLorenzo/antoninoLorenzo-codecrafters-shell-cpp

I’m new to C++ please be kind :sweat_smile:

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

1 Like

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

Suggestions:

  1. Use our CLI to test against previous stages by running:
codecrafters test --previous
  1. Focus on fixing the early stages first, as later stages depend on them.

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:

#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:

  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?

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:

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

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

1 Like

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