Comp Sci C++, where Programming Meets Chemistry.

Click For Summary
The discussion focuses on using C++ to calculate the molar mass of organic molecules based on user input. Participants emphasize the importance of understanding the algorithm before coding, particularly how to parse a string to identify elements and their quantities. The need for arrays and switch statements is highlighted, as well as the correct handling of numerical values associated with chemical symbols. Issues with the initial code are pointed out, including incorrect data types and logic errors in loops and conditionals. Overall, the conversation stresses the significance of pseudocode and algorithm development in programming before implementation.
  • #31
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
  • #32
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.
 
  • #33
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.
 
  • #34
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.
 
  • #35
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K