Oh. I'd be much obliged.
... I hope you don't mind if I post it all here. I tried uploading to MediaFire, and it doesn't want to take it for some reason. And I condensed it from 2 files.
Yeah, and I tried installing Code::Blocks today. Well, it even doesn't want to get installed, somehow managed to screw up dependencies inside one version. And g++ from the terminal doesn't do anything. It looks more and more like my system could use a fresh install.
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
const short ELEMENTS_ALLOWED = 96;
const char * filename = "./ElementsData.txt";
class Element
{
public:
Element() {}
~Element() {}
Element (short &, char * &, char * &, float &, short &, short (&)[7]);
void Display() const;
void AssignValues (short &, char (&)[20], char (&)[3], float &, short &, short (&)[7]);
short GetNumber() const { return itsNumber; }
char * GetName() const { return itsName; }
char * GetSymbol() const { return itsSymbol; }
float GetElectronegativity() const { return itsElectronegativity; }
short GetNofValencies() const { return itsNofValencies; }
short GetValencyNo(short &n) const { return itsValencies[n]; }
void DisplayValencies() const;
void readFromFile(ifstream &);
void writeToFile(ofstream &);
private:
short itsNumber;
char * itsName;
char * itsSymbol;
float itsElectronegativity;
short itsNofValencies;
short itsValencies[7];
};
Element::Element (short &number, char * &name, char * &symbol,
float &electronegativity, short &nofValencies, short (&valencies)[7])
{
itsNumber = number;
itsName = name;
itsSymbol = symbol;
itsElectronegativity = electronegativity;
itsNofValencies = nofValencies;
for(int i=0; i<nofValencies; i++)
{
itsValencies[i] = valencies[i];
}
}
void Element::Display() const
{
cout << "The element " << itsName << "'s number in the Periodical ";
cout << "Table is " << itsNumber << ".\nIts symbol is ";
cout << itsSymbol << ".\nIts electronegativity is ";
cout << itsElectronegativity << ".\n";
if (itsNofValencies==1)
{
cout << "It has a single valency which is ";
cout << itsValencies[0];
}
else
{
cout << "Its overall number of valencies is ";
cout << itsNofValencies << "and the valencies are ";
for(short i=0; i<itsNofValencies; i++)
{
cout << itsValencies[i];
if(i<(itsNofValencies-1))
cout << ", ";
else
cout << ".";
}
}
}
void Element::AssignValues (short &number, char (&name)[20], char (&symbol)[3],
float &electronegativity, short &nofValencies, short (&valencies)[7])
{
itsNumber = number;
itsName = name;
itsSymbol = symbol;
itsElectronegativity = electronegativity;
itsNofValencies = nofValencies;
for(int i=0; i<nofValencies; i++)
{
itsValencies[i] = valencies[i];
}
}
void Element::DisplayValencies() const
{
if (itsNofValencies==1)
{
cout << "Element " << itsName << " has a single valency which is ";
cout << itsValencies[0];
}
else
{
cout << "Element " << itsName << "'s overall number of valencies is ";
cout << itsNofValencies << "and the valencies are ";
for(short i=0; i<itsNofValencies; i++)
{
cout << itsValencies[i];
if(i<(itsNofValencies-1))
cout << ", ";
else
cout << ".\n\n";
}
}
}
void Element::readFromFile(ifstream &fin)
{
char temp[10];
fin.ignore(20, '[');
fin.getline(itsName, 21, ':');
fin.getline(temp, ':');
itsNumber = atoi(temp);
fin.getline(itsSymbol, 4, ':');
fin.getline(temp, ':');
itsElectronegativity = atof(temp);
fin.getline(temp, ':');
itsNofValencies = atoi(temp);
for(short i=0; i < itsNofValencies; i++)
{
if(i<(itsNofValencies-1))
{
fin.getline(temp, ',');
itsValencies[i] = atoi(temp);
}
else
{
fin.getline(temp, ']');
itsValencies[i] = atoi(temp);
}
}
}
void Element::writeToFile(ofstream &fout)
{
fout.put('[');
fout << itsName;
fout.put(':');
fout << itsNumber;
fout.put(':');
fout << itsSymbol;
fout.put(':');
fout << itsElectronegativity;
fout.put(':');
fout << itsNofValencies;
fout.put(':');
for(short i=0; i < itsNofValencies; i++)
{
fout << itsValencies[i];
if(i<(itsNofValencies-1))
fout.put(',');
}
fout << "]";
}
short menu(bool &file_fresh, short &elements_available)
{
short choice;
cout << "****Menu****\n\n";
if(elements_available>ELEMENTS_ALLOWED)
{
cout << "(-)Add an element.\n";
}
else
{
cout << "(1)Add an element.\n";
}
if(file_fresh)
{
cout << "(-)List the available elements by numbers.\n";
cout << "(-)List the available elements by names.\n";
cout << "(-)Choose an element to display, by number.\n";
cout << "(-)Choose an element to display, by name.\n";
}
else
{
cout << "(2)List the available elements by numbers.\n";
cout << "(3)List the available elements by names.\n";
cout << "(4)Choose an element to display, by number.\n";
cout << "(5)Choose an element to display, by name.\n";
}
cout << "(6)Quit.\n\n";
cout << ": ";
cin >> choice;
return choice;
}
int main(int argc, char** argv)
{
char name[20];
short number;
char symbol[3];
float electronegativity;
short nofValencies;
short valencies[7];
Element element_list[100];
short elements_available;
bool file_fresh;
ifstream fin;
ofstream fout;
fin.open(filename); //Checking if the file exists
fin.close();
if(fin.fail())
{
cout << "\nElements data file does not exist.\n"; //If the file does not exist,
fout.open(filename); //create it.
fout.close();
cout << "New file created.\n\n";
elements_available = 0;
file_fresh = true;
}
else
{ //If the file exists
fin.open(filename);
cout << "\nElements data file good.\n"; //print a message
file_fresh = false;
//fin.seekg(0, ios::beg);
for(short i=0; i==99; i++) //and read the data into the Elements_list
{
element_list[i].readFromFile(fin);
if(fin.peek()==fin.eof())
{
elements_available = i+1;
break;
}
}
if( elements_available>=ELEMENTS_ALLOWED ) //If there are 96 elements already
{ //(which is the number of elements naturally occurring on Earth)
cout << "Elements full!\n"; //print a message
}
cout << "\n";
fin.close();
}
short choice;
while((choice=menu(file_fresh, elements_available))!=6)
{
if ((choice == 1)&&(elements_available<ELEMENTS_ALLOWED))
{
cout << "Enter the desired element's name: ";
cin >> name;
cout << "\nEnter " << name << "'s number in the Periodical Table: ";
cin >> number;
cout << "\nEnter " << name << "'s symbol: ";
cin >> symbol;
cout << "\nEnter " << name << "'s electronegativity: ";
cin >> electronegativity;
cout << "\nEnter " << name << "'s overall number of valencies: ";
cin >> nofValencies;
for (short i = 0; i<nofValencies; i++)
{
cout << "Enter valency number " << i+1 << ": ";
cin >> valencies[i];
}
cout << "\n";
element_list[elements_available].AssignValues(number, name, symbol, electronegativity,
nofValencies, valencies);
elements_available++;
ofstream fout(filename, ios::app);
if( !fout )
{
cout << "Unable to open elements data for writing" << endl;
return 1;
}
fout.put('\n');
element_list[elements_available-1].writeToFile(fout);
cout << "Element saved.\n\n";
file_fresh = false;
fout.close();
}
else
if ((choice == 2)&&(!file_fresh))
{
cout << "Available elements' numbers are ";
for(short i=0; i<elements_available; i++)
{
cout << element_list[i].GetNumber();
if(i<(elements_available-1))
cout << ", ";
}
cout << ".\n\n";
}
else
if ((choice == 3)&&(!file_fresh))
{
cout << "Available elements are ";
for(short i=0; i<elements_available; i++)
{
cout << element_list[i].GetName();
if(i<(elements_available-1))
cout << ", ";
}
cout << ".\n\n";
}
else
if ((choice == 4)&&(!file_fresh))
{
cout << "Enter the desired element's number: ";
cin >> number;
cout << "\n";
for(short i=0; i<elements_available; i++)
{
if(element_list[i].GetNumber()==(number+1))
{
cout << "The data of the element No. ";
cout << number << ":\n\n";
element_list[i].Display();
cout << "\n";
break;
}
else
if(i==(elements_available-1))
cout << "The requested element not found.\n";
}
cout << "\n";
}
else
if ((choice == 5)&&(!file_fresh))
{
cout << "Enter the desired element's name: ";
cin >> name;
cout << "\n";
for(short i=0; i<elements_available; i++)
{
if(element_list[i].GetName()==name)
{
cout << "The data of the element ";
cout << name << ":\n\n";
element_list[i].Display();
cout << "\n\n";
break;
}
else
if(i==(elements_available-1))
cout << "The requested element not found.\n";
}
cout << "\n";
}
else
{
cout << "Option unavailable. Try again.\n\n";
}
}
return 0;
}