Thread: Program to Calculate Molar Mass

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Program to Calculate Molar Mass

    I need to write a C program to calculate the molar mass when given a chemical formula. For example: C6H13NO2.

    I've gotten my code to get the formula into a string and validate it. I've stored the possible atomic masses (C,H,N,O,S) in an array. And I'm using a for loop to process the values in the formula string.

    What I can't figure out is how to put the digits in the formula together as an integer value and multiply that value by the atomic mass represented by the preceding letter.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you looked into strtol?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Quote Originally Posted by tabstop View Post
    Have you looked into strtol?
    Yes, I am working with that, but it's giving me errors when I run the program. I'll keep working on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Need help with small program to calculate a salary
    By Guti14 in forum C++ Programming
    Replies: 13
    Last Post: 01-11-2004, 05:54 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM