int main()
{
Element e [112];
int num_elements;
num_elements = 0;
loadElements (e, num_elements);
menu (e, num_elements);
return 0;
}
/************************************************************
* void loadElements *
* This function will load an empty array with *
* data from a file. *
* *
************************************************************/
void loadElements ( Element element_array [], int& num_elements )
{
fstream inFile;
inFile.open ( "element_data.txt" , ios:: in );
Element e1;
string s; //placeholder for string
double d; //placeholder for double
int i; //placeholder for int
num_elements = 0; //counter variable
if (inFile.is_open())
{
while ( inFile.good() )
{
inFile >> s;
e1.setName (s);
inFile >> s;
e1.setSymbol (s);
inFile >> i;
e1.setAtomic_number (i);
inFile >> d;
e1.setAtomic_mass (d);
inFile >> d;
e1.setMelting_pt (d);
inFile >> d;
e1.setBoiling_pt (d);
inFile.ignore();
getline(inFile, s);
e1.setClassification (s);
inFile >> s;
e1.setCrystal_struct (s);
inFile >> d;
e1.setDensity (d);
inFile.ignore();
getline(inFile, s);
e1.setColor (s);
e1.computeProtonsElectrons(); //method used to calculate the
//numbers of protons & electrons
e1.computeNeutrons(); //method used to calculate the
//number of neutrons in an element
element_array [num_elements] = e1;
num_elements ++;
}
}
inFile.close();
}
// This is called in the menu function!! Can provide that
//code if needed as well (:
/************************************************************
* searchName *
* This function will search an array of elements *
* based on a name entered by the user *
* *
************************************************************/
void searchName ( Element e[], int num_elements )
{
bool found = false;
string input;
cout << "What word are you searching for? ";
cin >> input;
for ( int number = 0 ; number < 121 ; number ++)
{
while (input == e[number].getAtomic_number ())
{
found = true;
cout << "Element found!";
displayElement (e[number]); // displays the element
break;
}
while (e[number].getName () != input)
{
cout << "Element not found!" << endl;
break;
}
}
}
for ( int number = 0 ; number < 121 num_elements; number ++)
{
while (input == e[number].getAtomic_number ()) // string == int??
{found = true;cout << "Element found!";displayElement (e[number]); // displays the elementbreak; }while (e[number].getName () != input) // what is the purpose of these nested while loops?
if (input == e[number].getName())
{
cout << "Element found!" << endl;
return /*found*/;
}
}
cout << "Element not found!" << endl;
But its the loadElements function that is giving me trouble. It just crashes the program!
is markedly different than
It fails to search the file. I cannot pinpoint the problem.
Please be complete and accurate when you say you're having an issue. By crashes do you mean you get a segmentation fault? Unhandled exception? It hangs and becomes unresponsive?