jeudi 23 avril 2015

C++ Read the text file and find the word that has the highest number of occurrenc

I have a C++ issue. Currently i have a text file that store the following data. I wish to do a simple program where it will read the data and print out the most frequent display of item and the quantity of it.

I do not wish to set an array of fixed number of words to search through the file as the item entered into the text file is not predetermined

Below are the structure of the data in the text file

The fourth value is the quantity and the second value is the item name

Text File:

1|Bag|2|3|6|20Apr2015
2|Fruit|2.3|5|11.5|20Aug2015
4|Water|0.9|5|4.5|20Jun2015
5|File|0.9|5|4.5|20Jul2015
6|Water|0.9|5|4.5|20Nov2015
7|Water|0.9|5|4.5|20May2015
8|Bag|0.9|5|4.5|20Jan2015
9|Water|0.9|5|4.5|20Feb2015
10|Water|0.9|5|4.5|20Jan2015
11|Water|0.9|5|4.5|20Dec2015
12|File|0.9|5|4.5|20Oct2015
13|Water|0.9|5|4.5|20Sep2015
14|Water|0.9|5|4.5|20Sep2015

Desired Output:

The most frequent item is Water and 40 are sold

The least frequent item is Fruit and 5 are sold

Here is my current coding

    void computeItem()
    {


        string itemid;
        float totalprice;
        string item[4] ={"Water","Bag","Fruit","File"}; 
        int itemqty[4]={};

        int founditem;
        int foundyear;
        string year;
        string found;

        cout<<endl;


            for(int i = 0; i < MAX- 1; i++)
            {
                if(transactionpile[i].itemid !="")
                {
                    for(int k=0; k<4;k++)
                    {   

                            founditem = transactionpile[i].itemdesc.find(item[k]);
                            if(foundmonth<transactionpile[i].itemdesc.length())
                            {
                                //Add the totalprice of each found records to totalsalesmonth[k]
                                itemqty[k] += transactionpile[i].quantity;

                            }

                    }


                    }

                }


        }



        //Print out the output
        for(int i=0; i<4; i++)
        {

            cout << "            " << item[i] << "            " <<  << itemqty[i] <<endl;


        }

    }

Aucun commentaire:

Enregistrer un commentaire