Sorry, I realized that I put in all of my code in this question. All of my code equals most of the answer for this particular problem for other students, which was idiotic.
Here’s the basic gist of the problem I put:
I needed to recognize single digit numbers in a regular mathematical expression (such as 5 + 6) as well as double digit (such as 56 + 78). The mathematical expressions could also be displayed as 56+78 (no spaces) or 56 +78 and so on.
The actual problem was that I was reading in the expression as 5 6 + 7 8 no matter what the input was.
Thanks and sorry that I pretty much deleted this question, but my goal is not to give answers out for homework problems.
Jesse Smothermon
First, it helps to move such
ifs like thisinto its own function, just for the clarity.
Also, no need to check that it’s no operator, just check that the input is a number:
Another thing, with the long chain above, you got the problem that you will also enter the
tempNumber += ...block, if the input character is anyhing other than'+'. You would have to check with&&, or better with the function above:This will also rule out any invalid input like
b,Xand the likes.Then, for your problem with double digit numbers:
The problem is, that you always input a space after inserting the
tempNumber. You only need to do that, if the digit sequence is finished. To fix that, just modify the end of your longif-else ifchain:This should do the job of giving the correct representation from
56 + 78 --> 56 78 +. Please tell me if there’s anything wrong. 🙂