Thread: scanning the string

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    5

    scanning the string

    Hello, I have a little question, I need to write a function which receives a string as a parameter (like molecular formula "H2O, C6H12O6, ..." ) and has to do 2 things:
    1: has to check that an element always starts with a capital letter and may be followed by a single lower case character like Fe2O3
    2:the abscence of a number indicates a single atom of that element. for example CO2

    I was thinking of using string length method but don't seem to have any luck, any ideas?

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Use stringstream s.
    And break down the input into pieces, which would be easy to analyze individually.
    Here is an example of getting getting a string and a number from my username.
    Code:
    std::string input  "manasij7479";
    std::istringstream is(input);
    std::string name; 
    int number;
    is>>name>>number;
    if(!is)
        {/*Error*/}
    Last edited by manasij7479; 05-19-2012 at 02:11 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    5
    thanks for replaying, the thing is that the string can be like H2O, Fe2O3, CO2, not specifically letters followed by numbers

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Nino Meh View Post
    thanks for replaying, the thing is that the string can be like H2O, Fe2O3, CO2, not specifically letters followed by numbers
    So? Just think about it, you wont get anywhere unless you adapt and combine existing methods to solve your problems.
    You have to run a loop.
    Check for a string.
    If the stream fails, clear the error and get a number.
    Decide what to do, depending on whether it is a string or a number.

    You may need a state machine like structure for complex formulae.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    5
    this is what i have so far
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <fstream>
    #include <string>
    #include <cmath>
    #include <cctype>
    using namespace std;
    const int SIZE = 25;
    class CElement
    {
    public:
        string atomicSymbol;
        int atomicNumber;
        float atomicMass;
    };
    void readPeriodicTable(CElement [], int &);
    void bubbleSort(CElement [], int );
    void printTable(CElement [], int &);
    void promptUser();
    float numberOfGramrsPerMole(CElement [], string);
    int findMassNumber(string);
    double calculateNumberOfParticles(float, float);
    
    int main()
    {
        int num;
        float weight;
        string molecularFormula;
        CElement chemicalelement[SIZE];
        readPeriodicTable(chemicalelement, num);
        bubbleSort(chemicalelement, num);
        printTable(chemicalelement, num);
        promptUser();
        cin >> molecularFormula >> weight;
        return 0;
    }// end main
    
    /* Function readPeriodicTable: reads in the data from a file and puts it into an
     * array chemicalelement[]
     * Called by: main
     * Calls: none
     */
    void readPeriodicTable(CElement chemicalelement[], int &num)
    {
        int count;
        ifstream infile;
        infile.open("periodicTable.txt");
        if ( !infile.is_open() )
        {
            cout << endl << "ERROR: Unable to open file!" << endl;
            system("PAUSE");
            exit(1);
        }
        count = 0;
        while (!infile.eof() )
        {
            infile >> chemicalelement[count].atomicSymbol;
            infile >> chemicalelement[count].atomicNumber;
            infile >> chemicalelement[count].atomicMass;
            count++;
        }
        num = count;
        infile.close();
        return;
    }//end readPeriodicTable
    
    /* Function bubbleSort: recieves an array chemicalelement[], and sorts it in an
     * alphabetical order
     * Called by: main
     * Calles: none
     */
    void bubbleSort(CElement chemicalelement[], int num)
    {
        int j, pass;
        bool switched = true;
        CElement hold;
        for (pass = 0; pass < num - 1 && switched == true; pass++)
        {
            switched = false;
            for (j = 0; j < num - pass - 1; j++)
                if (chemicalelement[j].atomicSymbol > chemicalelement[j+1].atomicSymbol)
                {
                    switched = true;
                    hold = chemicalelement[j];
                    chemicalelement[j]= chemicalelement[j+1];
                    chemicalelement[j+1] = hold;
                }
        }
    }// end bubbleSort
    /* Function prinTable: recieves a sorted array chemicalelement[], and prints
     * neatly arranged table into a file output.txt
     * Called by: main
     * Calls: none
     */
    void printTable(CElement chemicalelement[], int &num)
    {
        ofstream outfile;
        outfile.open("output.txt");
        if(!outfile.is_open())
        {
            cout << endl << "ERROR; Unable to open file!" << endl;
            system("PAUSE");
            exit(1);
        }
        outfile << "Atomic Symbol    " << "Atomic Number    " << "Atomic Mass"
             << endl << endl;
        int count;
        for(count = 0; count < num; count++)
        {
            outfile.width(17);
            outfile << left << chemicalelement[count].atomicSymbol;
            outfile.width(17);
            outfile << left << chemicalelement[count].atomicNumber;
            outfile.width(17);
            outfile << left << chemicalelement[count].atomicMass << "\n";
        }
        return;
    }// end printTable
    void promptUser()
    {
        cout << "Enter the molecular formula: "<< endl;
        cout << "Enter the weight in grams: " << endl;
    }
    
    float numberOfGramrsPerMole(CElement chemicalelement[], string molecularFormula)
    {
        string s = molecularFormula, element1, element2;
        int subscript1, subscript2;
        
        for(int i = 0; i < s.length() ; i++) // not even close to solve the problem
        {
            if(s[i] == isupper()) 
            element1 = s[i];
            i++;
            else if(s[i] == isupper())
            element2 = s[i];
            subscript1 = 1;
        }
     
     
     
     
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanning the string
    By Nino Meh in forum C Programming
    Replies: 1
    Last Post: 05-19-2012, 02:40 PM
  2. Scanning string arrays from file
    By SimonBones in forum C Programming
    Replies: 4
    Last Post: 10-09-2011, 02:55 PM
  3. scanning and printing a string
    By Prestige in forum C Programming
    Replies: 5
    Last Post: 12-16-2010, 04:35 AM
  4. scanning in character string
    By zackboll in forum C Programming
    Replies: 12
    Last Post: 11-24-2004, 02:18 AM
  5. scanning a string twice???
    By Lynux-Penguin in forum C Programming
    Replies: 7
    Last Post: 05-12-2002, 08:06 PM