Classes and I/O

Okay, so I am trying to build a stoichiometry calculator. I really want it to have built in molar masses to simplify things for the user. I am using a class to do this and as far as I can tell it works well enough. Now when a user types in "grams to mols", equation will then equal grams to mols, and then that block will execute. When the user is asked for a compound, compound is the variable I use. How would I get it so that compound.molarmass will pull the appropriate variable from the Element class. The only sure fire way I know of would be if/elses, but I want to add a lot of compounds so that's out of the question for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include<string>
using namespace std;
string compound;
int gramsgiven;
string element;
int Molarmass;
string equation;
class Element

{
public:
  string symbol;
  int molarmass;
};
int main()
{
Element H, Li, Be, B, C, N, O, H2O;/*I will define the rest of these later*/
H.symbol = "H";
H.molarmass = 1.01;
Li.symbol = "Li";
Li.molarmass = 6.94;
Be.symbol = "Be";
Be.molarmass = 9.01;
O.symbol = "O";
O.molarmass = 16.00;
H2O.symbol = "He";
H2O.molarmass = 18.02;

cout << "What are you trying to solve for: " <<endl;
getline(cin, equation);

if (equation=="grams to mols")/*grams to mols converter*/
{
    cout << "Compound: " <<endl;
    getline(cin, compound);

    cout << "Grams given: " << endl;
    cin>>gramsgiven;

    Molarmass=gramsgiven / compound.molarmass;

    cout << "You have " << Molarmass << " grams of " << compound << endl;

}



}
Topic archived. No new replies allowed.