Skip to content

Conversation

sadeem-albir
Copy link
Contributor

The else part in "if (c == '-')" for some reason caused a calculation like "33 44 +" to evaluate to 7. After removing that else part, the output gives the desired result.

The else part in "if (c == '-')" for some reason caused a calculation like "33 44 +" to evaluate to 7. After removing that else part, the output gives the desired result.
@ohkimur
Copy link
Owner

ohkimur commented Feb 23, 2024

Hi @sadeem-albir! Welcome to the community. Do you think this is a bug? Can you check what happens in multiple cases what happens if we remove that else statement? If it's unnecessary, just delete it without any comments and push it here so that we can merge the fix in the main branch. Let me know what you think.

@sadeem-albir
Copy link
Contributor Author

Hi @sadeem-albir! Welcome to the community. Do you think this is a bug? Can you check what happens in multiple cases what happens if we remove that else statement? If it's unnecessary, just delete it without any comments and push it here so that we can merge the fix in the main branch. Let me know what you think.

Hi @ohkimur! Yes, it is probably a bug as I tried the program and was confused when inputting something like '33 44+' resulted in 7 instead of 77. I read through the program multiple times to see the issue, then I figured out why it was behaving that way. In that else statement, you getch() 'c', then when you move to the next section of code, you getch() c again, so whenever the program reads a number, it will read the first digit, then skip the second, and continue reading from third to last. By removing the else block, you prevent the program from looking over the second digit, and the bug is resolved.

Removed the last else-block from the "if (c == '-')" to prevent second digit from being skipped in the parsing process.
@ohkimur ohkimur merged commit 11bb243 into ohkimur:main Feb 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants