# \#NI6: Single quotes seemed to be parsed correctly but cat throws error (C++)

**URL:** <https://forum.codecrafters.io/t/ni6-single-quotes-seemed-to-be-parsed-correctly-but-cat-throws-error-c/16123>\
**Category:** Challenges\
**Tags:** challenge:shell\
**Created:** [May 21, 2026, 3:29pm UTC](https://forum.codecrafters.io/t/ni6-single-quotes-seemed-to-be-parsed-correctly-but-cat-throws-error-c/16123 "2026-05-21T15:29:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DailyPigeon](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dailypigeon/32/25639_2.png) [@DailyPigeon](https://forum.codecrafters.io/u/DailyPigeon)\
**Post date:** [May 21, 2026, 3:29pm UTC](https://forum.codecrafters.io/t/ni6-single-quotes-seemed-to-be-parsed-correctly-but-cat-throws-error-c/16123/1 "2026-05-21T15:29:07Z")

</div>

I’m stuck on Stage #NI6.

I have a “handleSingleQuotes” function takes the value of the ‘command’ variable which holds the user input. It appears to parse correctly according to the challenge’s instructions and echo test passes.

(I’m on macOS Tahoe 26.4.1 incase that helps.)

Here are my logs (all previous stages passed prior to this):

```auto
[tester::#NI6] Running tests for Stage #NI6 (Quoting - Single quotes)
[tester::#NI6] [setup] export PATH=/tmp/apple/raspberry/apple:$PATH
[tester::#NI6] Running ./your_program.sh
[tester::#NI6] [setup] echo -n "blueberry pear." > "/tmp/rat/f 50"
[tester::#NI6] [setup] echo -n "grape banana." > "/tmp/rat/f 90"
[tester::#NI6] [setup] echo "blueberry mango." > "/tmp/rat/f 9"
[your-program] $ echo 'test world'
[your-program] test world
[tester::#NI6] ✓ Received expected response
[your-program] $ echo world shell
[your-program] world shell
[tester::#NI6] ✓ Received expected response
[your-program] $ echo 'script example' 'shell''test' world''hello
[your-program] script example shelltest worldhello
[tester::#NI6] ✓ Received expected response
[your-program] $ cat '/tmp/rat/f 50' '/tmp/rat/f 90' '/tmp/rat/f 9'
[your-program] cat: /tmp/rat/f: No such file or directory
[tester::#NI6] ^ Line does not match expected value.
[tester::#NI6] Expected: "blueberry pear.grape banana.blueberry mango."
[tester::#NI6] Received: "cat: /tmp/rat/f: No such file or directory"
[your-program] cat: 50: No such file or directory
[your-program] cat: /tmp/rat/f: No such file or directory
[your-program] cat: 90: No such file or directory
[your-program] cat: /tmp/rat/f: No such file or directory
[your-program] cat: 9: No such file or directory
[your-program] $ 
[tester::#NI6] Test failed

```

And here are the snippets of my code:

handleSingleQuote (can’t pass by reference for some reason):

```cpp
std::string handleSingleQuote(std::string command) {
    if (command.find(39) != std::string::npos) {
        std::erase(command, '\'');
    } else {
        const auto new_end = std::ranges::unique(command, [](const char a, const char b) {
            return a == ' ' && b == ' ';
        }).begin();
        command.erase(new_end, command.end());
    }
    return command;
}

```

echo implementation:

```cpp
if (command.rfind("echo ", 0) == 0) {
            command.erase(0, 5);
            command = handleSingleQuote(command);
            std::cout << command << std::endl;
            std::cout << std::unitbuf;
        }

```

executable implementation:

```cpp
else if (isExecutable(program_name)) {
            command = handleSingleQuote(command);
            //std::cout << command << std::endl;
            //std::cout << std::unitbuf;
            pid_t pid = fork();
            if (pid == 0) {
                std::vector<char *> exec_argv;
                std::istringstream iss1(command);
                std::string arg;
                while (iss1 >> arg) {
                    exec_argv.push_back(strdup(arg.c_str()));
                }
                exec_argv.push_back(nullptr);
                execvp(exec_argv[0], exec_argv.data());
                std::cerr << command << ": command not found" << std::endl;
                std::cerr << std::unitbuf;
                exit(1);
            }
            // Parent process: wait for child
            waitpid(pid, nullptr, 0);
        }

```

Here’s the link to the repo: [https://git.codecrafters.io/4cb343607dfd3165](https://git.codecrafters.io/4cb343607dfd3165)

---

<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:** [May 22, 2026, 6:06am UTC](https://forum.codecrafters.io/t/ni6-single-quotes-seemed-to-be-parsed-correctly-but-cat-throws-error-c/16123/4 "2026-05-22T06:06:36Z")

</div>

Hey @DailyPigeon, for the single quotes stage, returning a single string from `handleSingleQuote` isn’t enough. You’ll need to parse the command into an array/vector of arguments instead.

For example, this input:

`cat '/tmp/rat/f 50' '/tmp/rat/f 90' '/tmp/rat/f 9'`

should be parsed as something like:

[`cat`, `'/tmp/rat/f 50'`, `'/tmp/rat/f 90'`, `'/tmp/rat/f 9'`]

One approach is to parse the input character-by-character while tracking whether you’re currently inside or outside quotes. Spaces should only split arguments when you’re outside quotes.

Let me know if you’d like me to clarify further.

---

<div class="post-metadata">

**Author:** ![DailyPigeon](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dailypigeon/32/25639_2.png) [@DailyPigeon](https://forum.codecrafters.io/u/DailyPigeon)\
**Post date:** [May 22, 2026, 11:48am UTC](https://forum.codecrafters.io/t/ni6-single-quotes-seemed-to-be-parsed-correctly-but-cat-throws-error-c/16123/6 "2026-05-22T11:48:39Z")

</div>

Hey thanks for the reply.

I think I kind of understand, so the directory and name need to be passed as a single argument. The directions just didn’t really make that clear for me 😅 .

I’ll let you know if I run into any more issues.

---

<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:** [May 27, 2026, 11:48am UTC](https://forum.codecrafters.io/t/ni6-single-quotes-seemed-to-be-parsed-correctly-but-cat-throws-error-c/16123/7 "2026-05-27T11:48:42Z")

</div>

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