I am working on a molecular mass calculator that can take just one string as the input. For example, you input H20 and it outputs 18.02. I know someone else has probably made another program that I could download, but I need the practice actually writing code. Currently I am getting an error when I input more more than H into the string, but I do not know what is causing this error. Any help as to why there is an error would be greatly appreciated because I am stuck.

Code:
``` :DCS "C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333" ClrHome 0->Z Input "FORMULA",Str1 1->X Repeat A=0 inString(Str1,"H",X->A inString(Str1,"e",X->B inString(Str1,"f",X->C X+1->X If A+1!=B and A!=0 Then 1.01*(expr(sub(Str1,A+1,1->Z End If A+1!=C and A!=0 Then 1.01*(expr(sub(Str1,A+1,1->Z End End 1->X Repeat A=0 inString(Str1,"He",X->A X+1->X If A!=0 Then 4*(expr(sub(Str1,A+2,1->Z End End 1->X Repeat A=0 inString(Str1,"Li",X->A X+1->X If A!=0 Then 6.94*(expr(sub(Str1,A+2,1->Z End End 1->X Repeat A=0 inString(Str1,"Be",X->A X+1->X If A!=0 Then 9.01*(expr(sub(Str1,A+2,1->Z End End Disp Z Pause ```
When you get this done i will definately use it, becase we are doing this in chemistry right at the present moment
As of now, I don't see the issue, but I do have a suggestion:

Code:
```If A+1!=B and A!=0 Then 1.01*(expr(sub(Str1,A+1,1->Z End ```

When you only have one command after an If statement, you do not need If...Then...End. Instead, make it

Code:
``` If A+1!=B and A!=0 1.01*(expr(sub(Str1,A+1,1->Z ```

You did this a few other times. I'll look over it again when I'm at home and have more time.[/quote]
gaventemples31415 wrote:
As of now, I don't see the issue, but I do have a suggestion:

Code:
```If A+1!=B and A!=0 Then 1.01*(expr(sub(Str1,A+1,1->Z End ```

When you only have one command after an If statement, you do not need If...Then...End. Instead, make it

Code:
``` If A+1!=B and A!=0 1.01*(expr(sub(Str1,A+1,1->Z ```

You did this a few other times. I'll look over it again when I'm at home and have more time.
[/quote]

Thank you! I think it might have something to do with my repeat commands because it only outputs 2.02 if I put H2H2, but I attempted to code it so it would find every instance of H.
This looks very solid so far; keep up the good work. I don't see any high-level optimizations to recommend, but if you're open to micro-optimizations, you can convert things like If A!= 0 to If not(A.
Thank you, Kerm. I'm still trying to get all of the bugs worked out right now. The optimizations will help because I think it will take a while once I input all of the elements.

Edit. I updated the code to add some of the optimizations that were given by Kerm and gaventemples31415. There is still a bug in the code though that I can't figure out. When I input any string with He in it gives a syntax error, but it will not take me to it when I press goto. Does anybody know what could be causing this error?

Code:
```:DCS "C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333C3C3C3C3C3C3C3C33333333333333333" ClrHome 0->Z Input "FORMULA ",Str1 1->X Repeat not(A inString(Str1,"H",X->A inString(Str1,"e",X->B inString(Str1,"f",X->C A+1->X If A+1!=B or A+1!=C and A!=0 Z+1.01*(expr(sub(Str1,A+1,1->Z End 1->X Repeat not(A inString(Str1,"He",X->A A+1->X If A!=0 Z+4*(expr(sub(Str1,A+2,1->Z End 1->X Repeat not(A inString(Str1,"Li",X->A A+1->X If A!=0 Z+6.94*(expr(sub(Str1,A+2,1->Z End 1->X Repeat not(A inString(Str1,"Be",X->A A+1->X If A!=0 Z+9.01*(expr(sub(Str1,A+2,1->Z End Disp Z Pause ```

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum