C++, where Programming Meets Chemistry.

  • Context: Comp Sci 
  • Thread starter Thread starter asz304
  • Start date Start date
  • Tags Tags
    Chemistry Programming
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
34 replies · 10K views
I appreciate your great effort for helping me in my last assignment, I compiled everything and they worked fine except for the example "C12". Thanks again
 
Physics news on Phys.org
Check what atoi() does.

Calling it chemistry is completely off, especially the idea of treating upper case and lower case interchangeably. Co and CO are two completely different things. I know it is not your fault; it doesn't make the assignment any better.
 
I passed my assignment online, I was almost done but decided to ignore inputs such as C12. But my atoi works fine for the other inputs.
 
Suppose you're at index 3 of a string and find that the character C is there. I.e., str[3] == 'C'. If the characters '1' and '2' follow 'C', you can get them with this expression: atoi(&str[4]).

&str[4] is the address of the character at index 4 of the string. atoi would take any numeric characters starting at index 4, and convert them to an int that it returns.
 
Shameless ad plug: if you want to see (reasonably) well designed and well written program that does real analysis of chemical formulas, go to my site (www.chembuddy.com), download EBAS, install it (risk free trial), start, click on "%" icon on the toolbar and enter any formula.

Note that the list of elements is editable - you can add moieties like "Acetate" so that your formulas are more readable (far easier to understand NaAcetate than NaC2H3O2). The real elements (those present in periodic table) are protected against edition. But it is not because they are treated differently internally, it required additional coding to prevent them from being modified.