# Help with Debugging Stage #NY8

**URL:** <https://forum.codecrafters.io/t/help-with-debugging-stage-ny8/1434>\
**Category:** Challenges\
**Tags:** challenge:grep\
**Created:** [August 19, 2024, 3:19am UTC](https://forum.codecrafters.io/t/help-with-debugging-stage-ny8/1434 "2024-08-19T03:19:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayraqutub](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/ayraqutub/32/1321_2.png) [@ayraqutub](https://forum.codecrafters.io/u/ayraqutub)\
**Post date:** [August 19, 2024, 3:19am UTC](https://forum.codecrafters.io/t/help-with-debugging-stage-ny8/1434/1 "2024-08-19T03:19:39Z")

</div>

I’m stuck on Stage #NY8.  
I am trying to implement ?, the zero or one quantifier. Here is the relevant snippet of my code.

```auto
else if (pattern[i]=='?'){
            if (i != 0) {
                //Check if the optional character matches the current character
                input_index--;
                if (input_index < input_line.length() && ((input_line[input_index] == pattern[i - 1]))) {
                    // If the optional character is present, move the index forward
                    std::cout << "input_line " << input_line[input_index] << "\n";
                    std::cout << "pattern " << pattern[i-1] << "\n";
                    input_index++;
                }else{
                    i++;
                }
                match = true;
            }
        } else {
            std::cout << "input_line " << input_line[input_index] << "\n";
            std::cout << "pattern " << pattern[i] << "\n";   
            match = match_char(input_line, input_index, pattern[i]); 
            std::cout << "match" << match << "\n";        
        }

```

I’ve added the couts to debug. It passes the first test. The logs for that test are as follows:

```auto
remote: [tester::#NY8] $ echo -n "cat" | ./your_program.sh -E "ca?t"
remote: [your_program] input_line c
remote: [your_program] pattern c
remote: [your_program] match1
remote: [your_program] input_line a
remote: [your_program] pattern a
remote: [your_program] match1
remote: [your_program] input_line a
remote: [your_program] pattern a
remote: [your_program] input_line t
remote: [your_program] pattern t
remote: [your_program] match1
remote: [tester::#NY8] ✓ Received exit code 0.

```

Note that match 1 means that there was a match and match 0 means that there wasn’t.  
Now for the next test, this fails and I am having trouble figuring out why. The logs for that test are as follows:

```auto
remote: [tester::#NY8] $ echo -n "dog" | ./your_program.sh -E "ca?t"
remote: [your_program] input_line d
remote: [your_program] pattern c
remote: [your_program] match0
remote: [your_program] input_line o
remote: [your_program] pattern a
remote: [your_program] match0
remote: [tester::#NY8] expected exit code 1, got 0
remote: [tester::#NY8] Test failed

```

If anyone has any idea of what I am doing wrong or can offer some guidance, I would really appreciate it. I’ve been stuck on this stage for a few days now. I’d be happy to share any more of my code if it helps.

Thanks!

---

<div class="post-metadata">

**Author:** ![jasonflorentino](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/jasonflorentino/32/1059_2.png) [@jasonflorentino](https://forum.codecrafters.io/u/jasonflorentino)\
**Post date:** [August 31, 2024, 7:32pm UTC](https://forum.codecrafters.io/t/help-with-debugging-stage-ny8/1434/2 "2024-08-31T19:32:15Z")

</div>

Hi there, Are you still having trouble with this? If so are you able to provide more detail or code? For example, I’m curious why the program doesn’t start testing the `input_line o` against `pattern c` after it fails to match `input_line d` with `pattern c` (and is instead proceeding to match as if the first character of the input matched the first character of the pattern).

---

<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:** [October 4, 2024, 1:54am UTC](https://forum.codecrafters.io/t/help-with-debugging-stage-ny8/1434/3 "2024-10-04T01:54:00Z")

</div>

Closing this thread due to inactivity. If you still need assistance, feel free to reopen or start a new discussion!

---

<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:** [October 9, 2024, 1:54am UTC](https://forum.codecrafters.io/t/help-with-debugging-stage-ny8/1434/4 "2024-10-09T01:54:43Z")

</div>

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