// This program will aid a chemistry student with the periodic table of elements. // The user can either input the atomic number or the element symbol. #include #include #include using namespace std; // Constants const int SYMBOL = 3; // Element symbols consist of two characters const int NAME = 20; // Number of characters an element name will have // Record structure for one element struct elementStruc { int atomicNumber; char elementSymbol[SYMBOL]; char elementName[NAME]; double atomicMass; }; // Function Prototypes void toLower(char string[]); void openCheck(ifstream& fileIn); int countData(ifstream& fileIn); void getData(ifstream& fileIn, elementStruc elements[]); int elementSymbolSearch(elementStruc elements[], int numElems, char value[]); int inputAtomicNumber(elementStruc elements[], int numElems); int inputElementSymbol(elementStruc elements[], int numElems); bool menu(elementStruc elements[], int numElems); void output(elementStruc elements[], int index); int main() { // Variable decleration ifstream fileCount, fileIn; //Two ifstreams one for counting the size int numElems; //and the other for the actual input bool cont; //Boolean to check if the user wants to continue // Open file fileCount.open("periodic.txt"); // Count the size of the file fileIn.open("periodic.txt"); // Inputs the elements of the file // Test for file existence both inFile and fileCount point to the same file openCheck(fileCount); // Count the number of elements numElems = countData(fileCount); // Array decleration elementStruc* elements; // Pointer for the elementStruc array elements = new elementStruc[numElems]; // Dynamic data allocation // Making space for the elemens array // Retrive data getData(fileIn,elements); // Menu cout << " THE PERIODIC TABLE OF ELEMENTS\n\n"; cont = menu(elements,numElems); //Menu Function while(cont) //While the user wants to continue cont = menu(elements,numElems); // Menu Function again // Deallocate data delete elements; return 0; } //******************************************************************* // This function reads integers from a file and stores the values in // an array of record structures. It returns the loaded array and // the number of elements in the array //******************************************************************* void getData(ifstream& fileIn, elementStruc elements[]) { //Declare and initialize int i = 0; //Initial input fileIn >> elements[0].elementSymbol >> elements[0].elementName >> elements[0].atomicNumber >> elements[0].atomicMass; // Test for end of file and array while (!fileIn.eof()) { i++; fileIn >> elements[i].elementSymbol >> elements[i].elementName >> elements[i].atomicNumber >> elements[i].atomicMass; } } //***************************************************************** // This function counts the number of elements in a text file // and returns the number of files in integer form //***************************************************************** int countData(ifstream& fileIn) { //Declare variables int i = 0; char elementSymbol[SYMBOL]; char elementName[NAME]; int atomicNumber; double atomicMass; fileIn >> elementSymbol >> elementName >> atomicNumber >> atomicMass; while (!fileIn.eof()) { i++; fileIn >> elementSymbol >> elementName >> atomicNumber >> atomicMass; } return i; } //***************************************************************** // This function checks to see if the file exists //***************************************************************** void openCheck(ifstream& fileIn) { if (fileIn.fail() ) { cout << "Problem opening file"; exit(-1); } } //***************************************************************** // This function prompts the user to input the element's // atomic number. It returns the index of the entered element // as an int. //***************************************************************** int inputAtomicNumber(elementStruc elements[], int numElems) { int value; cout << "Enter the atomic number of desired element: "; cin >> value; //Error checking number cannot be greater //than the number of elements while (value < 1 || value > numElems) { cout << "The element Symbol entered "; cout << "could not be found. Try again: "; cin >> value; } return value - 1; } //***************************************************************** // This function prompts the user to input the element symbol. // It returns the index of the entered element as an int. //***************************************************************** int inputElementSymbol(elementStruc elements[], int numElems) { char value[SYMBOL]; int search; cout << "Enter the element symbol of desired element: "; cin >> value; toLower(value); search = elementSymbolSearch(elements, numElems, value); // Error checking. while (search == -1) { cout << "The element Symbol entered "; cout << "could not be found. Try again: "; cin >> value; search = elementSymbolSearch(elements, numElems, value); } return search; } //******************************************************************** // The searchList function performs a linear search on an // integer array. The array list, which has a maximum of numElems // elements, is searched for the number stored in value. If the // number is found, its array subscript is returned. Otherwise, // -1 is returned indicating the value was not in the array. //******************************************************************** int elementSymbolSearch(elementStruc elements[], int numElems, char value[]) { int i = 0; // Used as a subscript to search array int position = -1; // To record position of search value bool found = false; // Flag to indicate if the value was found char target[SYMBOL]; while (i < numElems && !found) { // This converts the element symbol to lowercase chars. // This will allow the user to input either lowercase, // uppercase, or mixed element symbols. strcpy(target,elements[i].elementSymbol); toLower(target); // If strcmp is equal to zero then the element is found if (!(strcmp(target,value))) // ! indicates false or zero { found = true; // Set the flag position = i; // Record the value's subscript } i++; // Go to the next element } return position; // Return the position, or -1 } //***************************************************************** // This function converts a string variable into lowercase string // variable. //***************************************************************** void toLower(char string[]) { int i = 0; while (string[i] != '\0') { // Converts a single char into lowercase string[i] = char(tolower(string[i])); i++; } } //***************************************************************** // This is the output funcion. It recieves the elements array and // the index of the chosen element and prints out the information // corresponding that element from the array. //***************************************************************** void output(elementStruc elements[], int index) { cout << endl; cout << "SUMMARY" << endl; cout << "Element Name: " << elements[index].elementName << endl; cout << "Element Symbol: " << elements[index].elementSymbol << endl; cout << "Atomic Number: " << elements[index].atomicNumber << endl; cout << "Atomic Mass: " << elements[index].atomicMass << endl; } //***************************************************************** // This is the menu function. It is the second biggest function // of this program. It displays the whole meunu to the user // and prompts the user to see if he would like to continue. // If the user answers yes this funcion will return true, // otherwise it will return false. //***************************************************************** bool menu(elementStruc elements[], int numElems) { // Declare Variables char cont; int choice; int index = 0; // Present menu cout << "1. Search element by atomic number.\n"; cout << "2. Search element by element symbol.\n\n"; cout << "Chose either option 1 or 2 and press enter:"; // User input cin >> choice; // Error checking, user can only input 1 or 2 while (choice < 1 || choice > 2) { cout << "That is not an available choice. Try again:"; cin >> choice; } cout << endl; // The proper function is performed corresponding to the // users choice. if (choice == 1) index = inputAtomicNumber(elements,numElems); else index = inputElementSymbol(elements,numElems); // The output funcion, prints the summary output(elements,index); cout << endl; // Prompts the user for continuation cout << "Would you like to continue? Enter Y or N:"; cin >> cont; // User input is converted to lowercase cont = char(tolower(cont)); while (cont != 'y' && cont != 'n') { cout << "That is not an available choice. Try again:"; cin >> cont; cont = char(tolower(cont)); } cout << endl; // If user wants to continue return true, // otherwise return false. if (cont == 'y') return true; else return false; }