Periodic Table

Posts 1–15 of 17 · Page 1 of 2
Periodic Table
Ok, so i am making a win32 gui application that is all about chemistry. I just love chemistry, and it is my favorite subject in school, don't ask why (i like to make bombs ).

Obviously i need to include the periodic table in there somehow, so i decided to make a struct for elements, and it looks like this:

Code:
struct Element
{
       char* name;
       char* id;
       int atomic_number;
       double atomic_mass;
};

Now using this Element structure I define each element in the periodic table like this:

Code:
Element hydrogen;
hydrogen.name = "Hydrogen";
hydrogen.atomic_number = 1;
hydrogen.atomic_mass = 1.008;
hydrogen.id = "H";

Element lithium;
lithium.name = "Lithium";
lithium.atomic_number = 3;
lithium.atomic_mass = 6.94;
lithium.id = "Li";

// etc............

My problem is that if i use this method, it will take me a while to get the whole periodic table of elements recorded in my header file if i have to define each detail about each single one.

What i was wondering is how would you guys approach this.
Would you guys just go with the structure idea? or just recommend a class?

Or just something genious that hasn't come accross my mind.

Thanks in advance.
Code:
struct element {
  char*  name;
  char*  abbrev;
  int    atomic_number;
  double atomic_mass;

  element(char* name, char* abbrev, int atomic_number, double atomic_mass) : name(name), abbrev(abbrev), atomic_number(atomic_number), atomic_mass(atomic_mass) { }
}

element hydrogen("Hydrogen", "H", 1, 1.008);
Quote Originally Posted by Fovea View Post
Code:
struct element {
  char*  name;
  char*  abbrev;
  int    atomic_number;
  double atomic_mass;

  element(char* name, char* abbrev, int atomic_number, double atomic_mass) : name(name), abbrev(abbrev), atomic_number(atomic_number), atomic_mass(atomic_mass) { }
}

element hydrogen("Hydrogen", "H", 1, 1.008);


WHOA!!!!!!!!!!!!!!!

You are going to have to explain that code, that just looks so genious i just have to understand the logic behind it.
for simplicity sake i would just use a class.
Quote Originally Posted by 258456 View Post
WHOA!!!!!!!!!!!!!!!

You are going to have to explain that code, that just looks so genious i just have to understand the logic behind it.
What logic? |:

What the fuck, he's only done one thing: create a constructor (that doesn't do anything yet).

element hydrogen("haii");

typename variablename(constructor);
Quote Originally Posted by freedompeace View Post


What logic? |:

What the fuck, he's only done one thing: create a constructor (that doesn't do anything yet).

element hydrogen("haii");

typename variablename(constructor);
Wow, somebody has anger issues.


I meant this part:
Code:
element(char* name, char* abbrev, int atomic_number, double atomic_mass) : name(name), abbrev(abbrev), atomic_number(atomic_number), atomic_mass(atomic_mass) { }
I don't understand it from the colon and on.
Quote Originally Posted by freedompeace View Post


What logic? |:

What the fuck, he's only done one thing: create a constructor (that doesn't do anything yet).

element hydrogen("haii");

typename variablename(constructor);
He created a constructor that initialized the members of the struct. I believe that does something.

Quote Originally Posted by 258456 View Post
Wow, somebody has anger issues.


I meant this part:
Code:
element(char* name, char* abbrev, int atomic_number, double atomic_mass) : name(name), abbrev(abbrev), atomic_number(atomic_number), atomic_mass(atomic_mass) { }
I don't understand it from the colon and on.
It's called an initializer listⓘ and it basically gives member variables values.

name(name) will take the name passed in the arguments (char* name) and set the member variable called name to it. (The compiler is required to be smart enough to know you want to set the member, not the argument.)

Just think of ": name(name), " as "this->name = name;"
Quote Originally Posted by mmbob View Post


He created a constructor that initialized the members of the struct. I believe that does something.



It's called an initializer list and it basically gives member variables values.

name(name) will take the name passed in the arguments (char* name) and set the member variable called name to it. (The compiler is required to be smart enough to know you want to set the member, not the argument.)

Just think of ": name(name), " as "this->name = name;"

Thanks for explaining without senselessly saying the f word for no reason like somebody in this thread.
Quote Originally Posted by freedompeace View Post


What logic? |:

What the fuck, he's only done one thing: create a constructor (that doesn't do anything yet).

element hydrogen("haii");

typename variablename(constructor);
Hehe doesn't do nothing man, scroll sidewayz moar. Different way of constructing though, from what I usually see. I only use that style of construction when overloading constructors. Learned my something new for the day

As for the data entry, personally I'd just find some site that lists all the info on it, screen-scrape the page and then format it into a file, then go back to your current file and just loop through the file adding each new element as you come to it, probably would take as long as it would to enter them all in manually, but personally I'd rather be doing something interesting than something tedious
Forgot to add the const qualifier to the members of element (this is why I chose to use an initializer list). Elements should be immutable.

On a side note, there are two "initializer lists". The aforementioned one is not to be confused with the C++0x initializer list which is used to initialize containers.
Ok, what is going on with my compiler? Here is my elements.h file:

Code:
#ifndef ELEMENTS_H
#define ELEMENTS_H

struct  Element
{
       char* name;
       char* id;
       int atomic_number;
       double atomic_mass;
};




Element hydrogen;
hydrogen.name = "Hydrogen";
hydrogen.atomic_number = 1;
hydrogen.atomic_mass = 1.008;
hydrogen.id = "H";

Element lithium;
lithium.name = "Lithium";
lithium.atomic_number = 3;
lithium.atomic_mass = 6.94;
lithium.id = "Li";





#endif
These are my errors:

Code:
16 C:\Users\egmfco\Documents\Dev-CppPortable\Projects\Chemistry Program\elements.h expected constructor, destructor, or type conversion before '.' token 

16 C:\Users\egmfco\Documents\Dev-CppPortable\Projects\Chemistry Program\elements.h expected `,' or `;' before '.' token 

//etc... This error is for every line in which i am initializing my variables
I don't know what's going on. Any help is appreciated. Thank you.
Quote Originally Posted by 258456 View Post
Ok, what is going on with my compiler? Here is my elements.h file:

Code:
#ifndef ELEMENTS_H
#define ELEMENTS_H

struct  Element
{
       char* name;
       char* id;
       int atomic_number;
       double atomic_mass;
};




Element hydrogen;
hydrogen.name = "Hydrogen";
hydrogen.atomic_number = 1;
hydrogen.atomic_mass = 1.008;
hydrogen.id = "H";

Element lithium;
lithium.name = "Lithium";
lithium.atomic_number = 3;
lithium.atomic_mass = 6.94;
lithium.id = "Li";





#endif
These are my errors:

Code:
16 C:\Users\egmfco\Documents\Dev-CppPortable\Projects\Chemistry Program\elements.h expected constructor, destructor, or type conversion before '.' token 

16 C:\Users\egmfco\Documents\Dev-CppPortable\Projects\Chemistry Program\elements.h expected `,' or `;' before '.' token 

//etc... This error is for every line in which i am initializing my variables
I don't know what's going on. Any help is appreciated. Thank you.
At a guess I'd say it was because you never constructed an "Element"

Element Hydrogen

does not initialize the object.

Element Hydrogen = new Element();

probably would (assuming the 'new' keyword is used to construct objects in C++ as well)
Quote Originally Posted by Jason View Post


At a guess I'd say it was because you never constructed an "Element"

Element Hydrogen

does not initialize the object.

Element Hydrogen = new Element();

probably would (assuming the 'new' keyword is used to construct objects in C++ as well)
new is used with allocating memory in C++

classname* instance = new classname

you wouldnt use new without pointers because the memory is already allocated
nope that still didn't fix the problem
Ok, i realized that i can initialize them inside a function but not outside, the only thing is that i remember that i have done it many times before without these errors.
Posts 1–15 of 17 · Page 1 of 2

Post a Reply

Tags for this Thread

None

Talk with us