How to check the occurence of each words in the textfile and display the number of times C++ -


i have c++ issue. have text file store following data. wish simple program read data , print out frequent display of item , quantity of it.

i not wish set array of fixed number of words search through file item entered text file not predetermined

below structure of data in text file

the fourth value quantity , second value 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 frequent item water , 40 sold

the least frequent item fruit , 5 sold

here 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 = 0; < 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 totalprice of each found records totalsalesmonth[k]                                 itemqty[k] += transactionpile[i].quantity;                              }                      }                       }                  }           }            //print out output         for(int i=0; i<4; i++)         {              cout << "            " << item[i] << "            " <<  << itemqty[i] <<endl;           }      } 

maybe, while looping through file, dynamically grow std::vector of items? so, each time run across item, check see if it's in vector and, if isn't, push onto end. aside that, treat vector array you're using (i presume . . . can't tell you're doing now).


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -