Search code examples
cchemistry

Molecule mass calculator program in C


I need to do a proyect that calculates me the molar mass of any molecule entered by the user. For example if the user types CO2, my program needs to identify C (that is associated with a matrix with its mass), then identify O (associated with its mass) and multiply it by two, and add them up.

I was thinking of using strings of character for the each element.

Im very new in programing and I have learned just the basics pretty much.

how would you guys recommend me to do it? Im pretty lost

Thank you very much


Solution

  • It seems like the big challenge here is parsing the text, so you can pass full elements to a function to do your counting. You'll need a string to take the input, but I would strongly caution you away from the std string for this.

    Instead, try an array of char.

    Some things to consider:

    • How big does the array need to be?

    • What kind of characters will I accept? (spaces?)

    • How do I know when the user is done with input? (something something null-terminated...)

    • How will I move through the array?

    • What defines an element? (If it's capital letters, take a look at ASCII values for comparing them)

    • What do I do with an element when I find it?

    You asked for a starting place, so I'm reluctant to give out any specific code. Take a look at Jongware's comment for a good outline of what your program needs to do. Tackle it a piece at a time (answer the questions in this post and you'll be fine with input) and check it off your outline when you're done. Good luck!