c++ - Checking a file against itself -


i trying write program checks how many words inside of other words. tell user word has words in it. reason, while loop breaking , cannot file reload. ideas?

#include <iostream> #include <string> #include <fstream>  using namespace std;  struct finalword {     string word;     int number; };  ostream& operator<<(ostream& o, const finalword& f) {     return o << f.word << ": " << f.number; }  int main () {      finalword f;     string line, holder, line2;     ifstream myfile("enable1.txt");     int counter;     int holdlen;     size_t found;     if (myfile.is_open()){         while(getline(myfile,line)){             holder = line;             while (getline(myfile, line)){                 found = holder.find(line);                 if (found != string::npos){                     counter++;                 }                 if (counter > holdlen) {                     f.word = line;                     f.number = counter;                     holdlen = counter;                 }                 counter = 0;              }          }     }      cout << f << endl; } 

actually problem facing because of 2 loops using same myfile

in first while loop take word  in second while loop go through rest of file. 

it issue in following case.

if string "i happy" occurred more once iterate each occurrence search end of file.  avoid need take unique strings in vector said vaughn cato , check occurrence of string in whole file make efficient. 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -