Question:
how do i get my c++ to work? table of elements to choose from?
jai
2012-03-12 23:28:08 UTC
this is my project in school. i am trying to make a program that would compute for molarity and molality, using either grams or moles. when the user would give out grams, i was suppose to ask for which element so that the program i would make would compute for the corresponding moles. what to do ? :(


#include
#include
#include
#include
#include


using namespace std;

main ()
{

int x,y,a,b,c,d,g;
float m,l,Q;
string Z,Li,Be,B,C,N,O,F,Ne,Na,Mg,Al,Si,P,S,Cl,Ar,K,Ca,Ga,Ge,As,Se,Br,Kr,Rb,Sr,ln,Sn,Sb,Te,I,Xe,Cs,Ba,Tl,Pb,Bi,Po,At,Rn;

a=1;
b=2;
c=1;
d=2;

printf("This Program will compute for Molarity and Molality.\n\n");
printf(" Choose what you want this program to compute. \n\n type (1) Molarity and type (2) Molality. \n\n Enter your choice: ");
scanf("%d", &x);

{
if (x==a)

{
printf("\n\n You chose Molarity. \n\n");
printf(" Choose if you want to enter in (1) Grams (2) Moles: ");
scanf("%d",&y);
if (y==c)
{
printf("\n\nGRAMS\n");
printf("Enter Grams: ");
scanf("%d",&g);
system("cls");
printf(" ---- ---- \t ---- ---- ---- ---- ---- ---- \n");
printf(" | | | | \t | | | | | | | | | | | | \n");
printf(" |Li| |Be| \t |B | |C | |N | |O | |F | |Ne| You Entered %d grams\n",g);
printf(" | | | | \t | | | | | | | | | | | | \n");
printf(" ---- ---- \t ---- ---- ---- ---- ---- ---- \n");
printf(" | | | | \t | | | | | | | | | | | | \n");
printf(" |Na| |Mg| \t |Al| |Si| |P | |S | |Cl| |Ar| Choose which element\n");
printf(" | | | | \t | | | | | | | | | | | | you want to convert\n");
printf(" ---- ---- \t ---- ---- ---- ---- ---- ---- from grams to moles.\n");
printf(" | | | | \t | | | | | | | | | | | | Example: Al [enter]\n");
printf(" |K | |Ca| \t |Ga| |Ge| |As| |Se| |Br| |Kr|\n");
printf(" | | | | \t | | | | | | | | | | | |\n");
printf(" ---- ---- \t ---- ---- ---- ---- ---- ----\n");
printf(" | | | | \t | | | | | | | | | | | |\n");
printf(" |Rb| |Sr| \t |ln| |Sn| |Sb| |Te| |I | |Xe|\n");
printf(" | | | | \t | | | | | | | | | | | |\n");
printf(" ---- ---- \t ---- ---- ---- ---- ---- ---- \n");
printf(" | | | | \t | | | | | | | | | | | |\n");
printf(" |Cs| |Ba| \t |Tl| |Pb| |Bi| |Po| |At| |Rn|\n");
printf(" | | | | \t | | | | | | | | | | | |\n");
printf(" ---- ---- \t ---- ---- ---- ---- ---- ---- \n");
printf("Enter your Element here: ");
scanf("%s",&Z);
if (Z=="Li")
{

}
Five answers:
cja
2012-03-13 10:58:57 UTC
Your question says C++, but your code is C. I encourage you to use C++ for this. A lookup table will be the way to go, and C++'s STL map makes it a lot easier. Create a map with the periodic table data you need for your calculations, as I've done below. If you have to use C, it can still be done. The "map" will be an array, and you'll be on your own for lookup and access.



#include

#include

#include

#include



using namespace std;



struct element_t {

  string name;

  float atomicWeight;

  float density;

  float atomicVolume;

  // etc., any other attributes you need

  element_t(const string &s, float w, float d, float v) :

    name(s), atomicWeight(w), density(d), atomicVolume(v) { }

};

void initElementMap(map&);



ostream &operator<<(ostream &os, const element_t &e) {

  os << e.name << ": " << e.atomicWeight << ", " << e.density

        << ", " << e.atomicVolume;

  return os;

}



int main(int argc, char *argv[]) {

  string in;

  map elementMap;

  map::iterator i;



  initElementMap(elementMap);

  while (true) {

    cout << endl << "Enter element: ";

    getline(cin, in);

    if ((i = elementMap.find(in)) != elementMap.end()) {

      cout << i->second << endl;

      } else {

      cout << "?" << endl;

    }

  }

  return 0;

}



void initElementMap(map &eMap) {

  eMap.insert(make_pair("H", element_t("Hydrogen", 1.0079, 0.0000899, 14.4)));

  eMap.insert(make_pair("He", element_t("Helium", 4.0026, 0.0001787, 27.2)));

  eMap.insert(make_pair("Li", element_t("Lithium", 6.941, 0.53, 13.10)));

  // ...

}
gerrit
2016-11-29 04:50:46 UTC
Hmm, in case you meant element as in rely then the "air" answer isn't a part. in case you meant as in the 4 elementals, then honest adequate - alongside with fireplace water and earth. Silicone isn't a part, silicon in spite of the undeniable fact this is. For me, i could decide on Strontium because of the fact it makes the excellent purple colour in fireworks (Strontium nitrate)
?
2012-03-13 03:12:25 UTC
Only I can tell is that your program does not have any problem.

I am not chemist. If you explain I can help how to code further
?
2012-03-12 23:41:51 UTC
You are either going to need to use many if/else statements, or you can use a switch statement



Look here to see how they work: http://programmingnotes.freeweq.com/?p=268
Saajan
2012-03-13 00:09:54 UTC
the best way to use switch case. so that it ll be easy. to know more read @ www.cprogramming.com or www.cplusplus.com you ll find more hints.



regards

cjan


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...