# Interpreter challenge (C++): tester somehow fails to execute correctly else statement tests (stage ST5)

**URL:** <https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239>\
**Category:** Challenges\
**Tags:** challenge:interpreter\
**Created:** [April 5, 2025, 10:09am UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239 "2025-04-05T10:09:22Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 10:09am UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/1 "2025-04-05T10:09:22Z")

</div>

Hello, so I decided to tackle the interpreter challenge in C++, and for some reason, it fails at the second test (the random integer age one). Now, I usually take the test scripts and test them locally myself to see if I didn’t flub something up, but this time, while the tester fails, the local interpreter did not actually throw a parsing error and went through the script as the tester expected (tester got a ‘Expected ; after expression’ message for some reason. I cannot debug myself as this is a problem only reproducible on the testers. If you can access my code, then perhaps you can try compiling it on your end and see whether it works? (To get as close as possible to the tester’s condition, I elected to build and debug using WSL.)

In desperation, I tried adding some hacky checks for the tester so it can proceed anyway, so don’t mind the macro in main.cpp. (I managed to get past that step of the challenge after setting a hacky patch up, but I will remove said patch once the challenge is finished.)

Here is the test Lox script:

```auto
var age = 30; // The integer actually changes
if (age > 18) print "adult"; else print "child";

```

---

<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:** [April 5, 2025, 4:34pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/4 "2025-04-05T16:34:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 4:36pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/5 "2025-04-05T16:36:02Z")

</div>

Okay, I’ll do it as soon as possible! I don’t actually know how to do this, though, do I just share from what I currently have?

---

<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:** [April 5, 2025, 4:43pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/6 "2025-04-05T16:43:23Z")

</div>

I do have access to your current code that includes the hack. It would be super helpful if you could also share a version that works locally **without** the hack.

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 5:05pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/7 "2025-04-05T17:05:27Z")

</div>

Well, you can comment out the define macro. That’s all that needs to be done for it to work. Do you want me to make a commit for this?

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 5:10pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/8 "2025-04-05T17:10:57Z")

</div>

None of the patches will run if the CODECRAFTERS\_PATCHES macro is not defined

---

<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:** [April 5, 2025, 5:19pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/9 "2025-04-05T17:19:34Z")

</div>

```auto
var age = 31;
if (age > 18) print "adult"; else print "child";

```

OK, I commented out the CODECRAFTERS\_PATCHES macro. The error seems to happen in `get_expr_statement`:

`consume(TokenType::SEMICOLON, "Expected ';' after expression???");`  
`check(token_type))`

Inside check, I tried printing **peek().get\_token\_type()** and **type** , but couldn’t get it to compile.

```auto
    bool Parser::check(TokenType type){
        if (is_at_end()){
            return false;
        }
        return peek().get_token_type() == type;
    }

```

Any suggestions on how to inspect these values properly? @fortwoone

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 5:24pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/10 "2025-04-05T17:24:05Z")

</div>

Hmm, weird. How did you print it? Did you use cout or printf?

---

<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:** [April 5, 2025, 5:58pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/11 "2025-04-05T17:58:05Z")

</div>

I had trouble printing stuff, but here’s what I got so far:

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

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

Tokens have unexpected “adult child” at the beginning. Investigating how they got into tokens.

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 6:00pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/12 "2025-04-05T18:00:14Z")

</div>

I see, thanks for taking a look for me!

---

<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:** [April 5, 2025, 6:15pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/13 "2025-04-05T18:15:58Z")

</div>

Quick update: they came from within the comments:

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

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/c/cb1367aa6e26c5bfb7aef15ba3098c66cdc1b251.jpeg)

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/a/ae2626698d25104230b6b40d09bac190a0023d65.jpeg)

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

---

<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:** [April 5, 2025, 6:29pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/14 "2025-04-05T18:29:33Z")

</div>

@fortwoone Feel free to test locally using quoted strings inside comments:

```auto
// This program initializes age with a random integer and then prints "adult"
// if the age is greater than 18, otherwise it prints "child"
var age = 42;
if (age > 18) print "adult"; else print "child";

```

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/2/2852a655ade39ffb9dbca1e16a26f471d3499653.jpeg)

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 6:39pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/16 "2025-04-05T18:39:18Z")

</div>

…ARGH, RIGHT. Well, oops. Will try fixing that.Turns out it’s not a bug from your end… sorry for disturbing

---

<div class="post-metadata">

**Author:** ![fortwoone](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/fortwoone/32/8614_2.png) [@fortwoone](https://forum.codecrafters.io/u/fortwoone)\
**Post date:** [April 5, 2025, 6:42pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/17 "2025-04-05T18:42:44Z")

</div>

The fix is actually pretty stupid in hindsight as well, apologies

---

<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:** [April 13, 2025, 11:05pm UTC](https://forum.codecrafters.io/t/interpreter-challenge-c-tester-somehow-fails-to-execute-correctly-else-statement-tests-stage-st5/8239/18 "2025-04-13T23:05:28Z")

</div>

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