Having trouble with the counter and functions
ok here is my assignment: You are to write the following functions:

checkNumber – This function will receive an atomicNumber as a parameter (call by value)and will check whether the atomicNumber is a valid number. Valid numbers are the values 1-118. If the number is valid, it should print an appropriate message and return true; otherwise it should print an appropriate error message and return false.

readData – This function receives an atomicNumber (call by value)  and returns the corresponding  name, atomicWeight, and period (call by reference). Each time the function is invoked it opens the file and searches for the corresponding entry in the data file.

printName – This function receives an element’s name and prints a heading and the name of that element

isNobleGas – This function receives an integer representing the element’s period (call by value) and returns true if the element is a noble gas and false otherwise. Elements in group 18 are noble gasses.

isAlkaliMetal – This function receives an integer representing the element’s period (call by value) and returns true if the element is an alkali metal and false otherwise. Elements in group 1 are alkali metals.

isAlkaline – This function receives an integer representing the element’s period (call by value) and returns true if the element is an alkaline earth metal and false otherwise. Elements in group 2 are alkaline earth metals.

atomicParticles – This function receives an element’s atomicNumber and atomicWeight (call by value) and returns the number of protons, neutrons and electrons in the atom of that element (call by reference). Recall that:

number of protons = number of electrons = atomic number

number of neutrons = atomic weight – atomic number

classify – This functions will receive an atomicNumber (call by value) and call the function readData to obtain that element’s relevant information. If the information is not in the PeriodicTable.txt data file, it should print the message: “Element not listed yet.”

In all other cases it is to call the functions printName, isNobleGas, IsAlkaliMetal, isAlkaline and atomicParticles in order to print the appropriate information. Note: with the exception of printName, all printing is to be done by the classify function. Also note, that an element cannot be classified as in more than one group (e.g., it cannot be both a noble gas and also an alkali metal), so you should be using if…else statements. If the element is not a noble gass and is not an alkali metal and also is not an alkaline earth metal, the function should print “Not classified yet.”

main – The main function is to use a loop to process date contained in an input file. An atomic number is to be read in. The main function should call the function checkNumber to check that the atomic number is valid. If the atomic number is valid it should call the function classify to print out all the information (as described above) for that element. However, if the element number is not valid, an appropriate message will be printed (by checkNumber) and go on to the next input.

When all the input has been processed the main function should print the total number of input elements processed, and how many inputs were valid and how many inputs were invalid.

 

Input data:

Create a data file that contains the following entries:

            2

            120

            20

            19

            79

 

Make up 10 more elements that will test your program. Include some invalid data (both too small and too large) and include some elements whose names have not been listed yet as well as some whose types have not been classified yet.

 

Sample Output:

For the data above your program is to print:

Element #2: Helium

Period 1

Classification: Noble Gas

Number of Protons: 2

Number of Neutrons: 2

Number of electrons: 2

 

Element #120: Invalid data

 

Element #20: Calcium

Period 4

Classification: Alkaline Earth Metal

Number of Protons: 20

Number of Neutrons: 20

Number of electrons: 20

 

Element #19: Name not listed yet

 

Element #79: Gold

Period 6

Classification: Not classified yet

Number of Protons: 79

Number of Neutrons: 118

Number of electrons: 79

 

 

 








and this is my code:

    /*
    Name: Homework #4 Periodic Table
    Date: 11/05/11
    Description: This program's purpose is to get us used to using
                 user-defined functions in order to accomplish a task.
    */
    #include <iostream>
    #include <fstream>
    #include<cstdlib>
    #include<cmath>
    
    using namespace std;
    
    bool checkNumber (int);
    void classify (int);
    void printName(int,string);
    bool isNobleGas (int);
    bool isAlkaliMetal (int);
    bool isAlkaline (int);
    void readData (int,string&,int&,int&,int&);
    int atomicParticles (int,int,int&,int&,int&);
    
    ofstream outfile;
    
    int main()
    {
        int atomicNumber, countValid = 0, countInvalid = 0, total = 0;
    
        outfile.open("results.txt");
        outfile.setf(ios::fixed,ios::floatfield);
    
        ifstream infile;
        infile.open("atomicnumbers.txt");
    
        if (!infile.is_open()) {
            cout << "ERROR: Unable to open file atomicnumbers.txt" << endl;
            system("PAUSE");
            exit(1);
        }
    
        infile >> atomicNumber;
    
        while(!infile.eof()){
            checkNumber(atomicNumber);
    
            if (atomicNumber == true){
                countValid++;
                classify(atomicNumber);
            }
            else
                countInvalid++;
    
            total++;
            infile >> atomicNumber;
        }
    
        outfile << "The total number of input elements processed was "
                << total << endl;
        outfile << countValid << " were valid." << endl;
        outfile << countInvalid << " were invalid." << endl;
    
        infile.close();
        outfile.close();
    return 0;
    }//end main
    
    //receives a possible atomic number, determines validity
    bool checkNumber (int atomicNumber){
    
        if( atomicNumber >= 1 && atomicNumber <= 118 ){
            outfile << "Atomic Number " << atomicNumber << " is a" <<
                       " valid entry. \n";
            return true;
        }
        else {
            outfile << "Atomic Number " << atomicNumber << " is an " <<
                       "invalid entry. \n";
            return false;
        }
    }//end checkNumber
    
    //Receives an atomic number(value), calls readData for information
    //(reference). If not found, prints message element not
    //classified yet. If found, calls printName, isNobleGas, isAlkaliMetal
    //isAlkaline, and atomicParticles to further classify the element
    void classify (int atomicNumber){
    
        string element;
        int matchAtomicNumber, atomicWeight, group, period;
        int protons, neutrons, electrons;
    
        readData(atomicNumber, element, atomicWeight,
                     group, period);
    
        if (element == "notlisted")
            outfile << "Element #" << atomicNumber
                    << ": Name not listed yet \n";
        else printName(atomicNumber, element);
    
        outfile << "Period " << period;
    
        if (isNobleGas)
            outfile << "Classification: Noble Gas";
        else if (isAlkaliMetal)
                outfile << "Classification: Alkali Metal";
             else if (isAlkaline)
                    outfile << "Classification: Alkaline Earth Metal";
                  else
                    outfile << "Classification: Not classified yet";
    
        atomicParticles(atomicNumber,atomicWeight, protons,
                          electrons, neutrons);
    
        outfile << "Number of Protons: " << protons << endl;
        outfile << "Number of Neutrons: " << neutrons << endl;
        outfile << "Number of Electrons: " << electrons << endl << endl;
    
    }// end classify
    
    void readData(int atomicNumber, string &element, int &atomicWeight,
                  int &group, int &period)
    {
        int matchAtomicNumber;
    
        ifstream infile;
        infile.open("Periodictable.txt");
    
        if (!infile.is_open()) {
            cout << "ERROR: Unable to open file Periodictable.txt" << endl;
            system("PAUSE");
            exit(1);
        }
        infile.clear();
    
        infile >> matchAtomicNumber >> element >> atomicWeight
               >> group >> period;
    
        while (!infile.eof()){
            if (matchAtomicNumber != atomicNumber)
                infile >> matchAtomicNumber >> element >> atomicWeight
                       >> group >> period;
         }// end while
    
        if (matchAtomicNumber != atomicNumber)
            element = "notlisted";
        infile.close();
    }//end readData
    
    //receives an elements atomic number and name, and prints a heading
    void printName(int atomicNumber, string element){
        outfile << "Element#"<< atomicNumber << ": " << element;
    }//end printName
    
    //checks if an element is a noble gas
    bool isNobleGas(int &group){
    if (group == 18)
        return true;
    }//end isNobleGas
    
    //checks if an element is an alkali metal
    bool isAlkaliMetal (int &group){
        if(group == 1)
            return true;
    }//end isAlkaliMetal
    
    //checks if an element is an alkaline earth metal
    bool isAlkaline (int &group){
        if(group == 2)
            return true;
    }//end isAlkaline
    
    //receives an atomic number and atomic weight (value), and determines
    //the number of protons electrons and neutrons (reference)
    int atomicParticles (int atomicNumber, int atomicWeight, int &protons,
                         int &electrons, int &neutrons)
    {
        protons = electrons = atomicNumber;
        neutrons = atomicWeight - atomicNumber;
    
    }//end atomicParticles




question: for some reason my counters for valid and invalid aren't working... the whole entire program seems to stop working at line 47... can anyone see what im doing wrong?

Having trouble with the counter and functions
===

ok here is my assignment: You are to write the following functions:

checkNumber – This function will receive an atomicNumber as a parameter (call by value)and will check whether the atomicNumber is a valid number. Valid numbers are the values 1-118. If the number is valid, it should print an appropriate message and return true; otherwise it should print an appropriate error message and return false.

readData – This function receives an atomicNumber (call by value)  and returns the corresponding  name, atomicWeight, and period (call by reference). Each time the function is invoked it opens the file and searches for the corresponding entry in the data file.

printName – This function receives an element’s name and prints a heading and the name of that element

isNobleGas – This function receives an integer representing the element’s period (call by value) and returns true if the element is a noble gas and false otherwise. Elements in group 18 are noble gasses.

isAlkaliMetal – This function receives an integer representing the element’s period (call by value) and returns true if the element is an alkali metal and false otherwise. Elements in group 1 are alkali metals.

isAlkaline – This function receives an integer representing the element’s period (call by value) and returns true if the element is an alkaline earth metal and false otherwise. Elements in group 2 are alkaline earth metals.

atomicParticles – This function receives an element’s atomicNumber and atomicWeight (call by value) and returns the number of protons, neutrons and electrons in the atom of that element (call by reference). Recall that:

number of protons = number of electrons = atomic number

number of neutrons = atomic weight – atomic number

classify – This functions will receive an atomicNumber (call by value) and call the function readData to obtain that element’s relevant information. If the information is not in the PeriodicTable.txt data file, it should print the message: “Element not listed yet.”

In all other cases it is to call the functions printName, isNobleGas, IsAlkaliMetal, isAlkaline and atomicParticles in order to print the appropriate information. Note: with the exception of printName, all printing is to be done by the classify function. Also note, that an element cannot be classified as in more than one group (e.g., it cannot be both a noble gas and also an alkali metal), so you should be using if…else statements. If the element is not a noble gass and is not an alkali metal and also is not an alkaline earth metal, the function should print “Not classified yet.”

main – The main function is to use a loop to process date contained in an input file. An atomic number is to be read in. The main function should call the function checkNumber to check that the atomic number is valid. If the atomic number is valid it should call the function classify to print out all the information (as described above) for that element. However, if the element number is not valid, an appropriate message will be printed (by checkNumber) and go on to the next input.

When all the input has been processed the main function should print the total number of input elements processed, and how many inputs were valid and how many inputs were invalid.

 

Input data:

Create a data file that contains the following entries:

            2

            120

            20

            19

            79

 

Make up 10 more elements that will test your program. Include some invalid data (both too small and too large) and include some elements whose names have not been listed yet as well as some whose types have not been classified yet.

 

Sample Output:

For the data above your program is to print:

Element #2: Helium

Period 1

Classification: Noble Gas

Number of Protons: 2

Number of Neutrons: 2

Number of electrons: 2

 

Element #120: Invalid data

 

Element #20: Calcium

Period 4

Classification: Alkaline Earth Metal

Number of Protons: 20

Number of Neutrons: 20

Number of electrons: 20

 

Element #19: Name not listed yet

 

Element #79: Gold

Period 6

Classification: Not classified yet

Number of Protons: 79

Number of Neutrons: 118

Number of electrons: 79

 

 

 








and this is my code:

    /*
    Name: Homework #4 Periodic Table
    Date: 11/05/11
    Description: This program's purpose is to get us used to using
                 user-defined functions in order to accomplish a task.
    */
    #include <iostream>
    #include <fstream>
    #include<cstdlib>
    #include<cmath>
    
    using namespace std;
    
    bool checkNumber (int);
    void classify (int);
    void printName(int,string);
    bool isNobleGas (int);
    bool isAlkaliMetal (int);
    bool isAlkaline (int);
    void readData (int,string&,int&,int&,int&);
    int atomicParticles (int,int,int&,int&,int&);
    
    ofstream outfile;
    
    int main()
    {
        int atomicNumber, countValid = 0, countInvalid = 0, total = 0;
    
        outfile.open("results.txt");
        outfile.setf(ios::fixed,ios::floatfield);
    
        ifstream infile;
        infile.open("atomicnumbers.txt");
    
        if (!infile.is_open()) {
            cout << "ERROR: Unable to open file atomicnumbers.txt" << endl;
            system("PAUSE");
            exit(1);
        }
    
        infile >> atomicNumber;
    
        while(!infile.eof()){
            checkNumber(atomicNumber);
    
            if (atomicNumber == true){
                countValid++;
                classify(atomicNumber);
            }
            else
                countInvalid++;
    
            total++;
            infile >> atomicNumber;
        }
    
        outfile << "The total number of input elements processed was "
                << total << endl;
        outfile << countValid << " were valid." << endl;
        outfile << countInvalid << " were invalid." << endl;
    
        infile.close();
        outfile.close();
    return 0;
    }//end main
    
    //receives a possible atomic number, determines validity
    bool checkNumber (int atomicNumber){
    
        if( atomicNumber >= 1 && atomicNumber <= 118 ){
            outfile << "Atomic Number " << atomicNumber << " is a" <<
                       " valid entry. \n";
            return true;
        }
        else {
            outfile << "Atomic Number " << atomicNumber << " is an " <<
                       "invalid entry. \n";
            return false;
        }
    }//end checkNumber
    
    //Receives an atomic number(value), calls readData for information
    //(reference). If not found, prints message element not
    //classified yet. If found, calls printName, isNobleGas, isAlkaliMetal
    //isAlkaline, and atomicParticles to further classify the element
    void classify (int atomicNumber){
    
        string element;
        int matchAtomicNumber, atomicWeight, group, period;
        int protons, neutrons, electrons;
    
        readData(atomicNumber, element, atomicWeight,
                     group, period);
    
        if (element == "notlisted")
            outfile << "Element #" << atomicNumber
                    << ": Name not listed yet \n";
        else printName(atomicNumber, element);
    
        outfile << "Period " << period;
    
        if (isNobleGas)
            outfile << "Classification: Noble Gas";
        else if (isAlkaliMetal)
                outfile << "Classification: Alkali Metal";
             else if (isAlkaline)
                    outfile << "Classification: Alkaline Earth Metal";
                  else
                    outfile << "Classification: Not classified yet";
    
        atomicParticles(atomicNumber,atomicWeight, protons,
                          electrons, neutrons);
    
        outfile << "Number of Protons: " << protons << endl;
        outfile << "Number of Neutrons: " << neutrons << endl;
        outfile << "Number of Electrons: " << electrons << endl << endl;
    
    }// end classify
    
    void readData(int atomicNumber, string &element, int &atomicWeight,
                  int &group, int &period)
    {
        int matchAtomicNumber;
    
        ifstream infile;
        infile.open("Periodictable.txt");
    
        if (!infile.is_open()) {
            cout << "ERROR: Unable to open file Periodictable.txt" << endl;
            system("PAUSE");
            exit(1);
        }
        infile.clear();
    
        infile >> matchAtomicNumber >> element >> atomicWeight
               >> group >> period;
    
        while (!infile.eof()){
            if (matchAtomicNumber != atomicNumber)
                infile >> matchAtomicNumber >> element >> atomicWeight
                       >> group >> period;
         }// end while
    
        if (matchAtomicNumber != atomicNumber)
            element = "notlisted";
        infile.close();
    }//end readData
    
    //receives an elements atomic number and name, and prints a heading
    void printName(int atomicNumber, string element){
        outfile << "Element#"<< atomicNumber << ": " << element;
    }//end printName
    
    //checks if an element is a noble gas
    bool isNobleGas(int &group){
    if (group == 18)
        return true;
    }//end isNobleGas
    
    //checks if an element is an alkali metal
    bool isAlkaliMetal (int &group){
        if(group == 1)
            return true;
    }//end isAlkaliMetal
    
    //checks if an element is an alkaline earth metal
    bool isAlkaline (int &group){
        if(group == 2)
            return true;
    }//end isAlkaline
    
    //receives an atomic number and atomic weight (value), and determines
    //the number of protons electrons and neutrons (reference)
    int atomicParticles (int atomicNumber, int atomicWeight, int &protons,
                         int &electrons, int &neutrons)
    {
        protons = electrons = atomicNumber;
        neutrons = atomicWeight - atomicNumber;
    
    }//end atomicParticles




question: for some reason my counters for valid and invalid aren't working... the whole entire program seems to stop working at line 47... can anyone see what im doing wrong?
