xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Well the code still does this so I'm not surprised it doesn't accept negative numbers:
Code:
                if((input_string[r]=='+')||(input_string[r]=='-'))
                {
                     if((input_string[r+1]=='+')||(input_string[r+1]=='-')||(input_string[r+1]=='*')||(input_string[r+1]=='/'))
                     {
                         return false;
because this specifically excludes something like "3+-5".

Expression evaluation code is not in general simple to write, and more difficult to read and modify particularly when the code has as few comments as yours does. So I can't really make any suggestions except that you think carefully through what it is supposed to do, with pen and paper away from the computer. Decide on an initial algorithm, write several expressions out and work through that algorithm as the computer would: character by character, keeping track of variable contents, and following the algorithm strictly, being careful to do *exactly* what the algorithm states and NOT what you think it means. This isn't easy, and perhaps it would be helpful to talk through it with someone who can point out when you're doing what you meant rather than what you have written down.

You can then use that process to modify the algorithm so that it works with the example expressions you've thought up.

This is called a "dry run" and is a VERY important skill that all programmers *need*.
gabuchia's Avatar, Join Date: Oct 2009
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Well the code still does this so I'm not surprised it doesn't accept negative numbers:
Code:
                if((input_string[r]=='+')||(input_string[r]=='-'))
                {
                     if((input_string[r+1]=='+')||(input_string[r+1]=='-')||(input_string[r+1]=='*')||(input_string[r+1]=='/'))
                     {
                         return false;
because this specifically excludes something like "3+-5".

Expression evaluation code is not in general simple to write, and more difficult to read and modify particularly when the code has as few comments as yours does. So I can't really make any suggestions except that you think carefully through what it is supposed to do, with pen and paper away from the computer. Decide on an initial algorithm, write several expressions out and work through that algorithm as the computer would: character by character, keeping track of variable contents, and following the algorithm strictly, being careful to do *exactly* what the algorithm states and NOT what you think it means. This isn't easy, and perhaps it would be helpful to talk through it with someone who can point out when you're doing what you meant rather than what you have written down.

You can then use that process to modify the algorithm so that it works with the example expressions you've thought up.

This is called a "dry run" and is a VERY important skill that all programmers *need*.
If I were to remove the checking of 5--5, it would give me an error, if I enter that expression. Runtime error to be precise, any other suggestions?
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Solving this is not going to be as simplistic as just removing a bit of code. This is a complex project and you NEED to study what I suggested in my last post. If you don't want to do that then this project is beyond your abilities and you should look for something simpler.