I know its my fault it doesnt work, I have only been learning c++ for a month. I did pretty good with this except when it reads the file the word count is always 0.
This is supposed to look at a user defined file and count the number of words in it and like I said even though the file has multiple words the result is always 0.
Code:#include<iostream> #include<cstdlib> #include<fstream> #include<iomanip> #include<cctype> #include<string> #include<vector> #include<cstdlib> using namespace std; class FileReader { public: FileReader(){counted=false, word_counter=0;} void reset(); void load_ignore_file(); void add_ignore(); void file_count(); int menu(); private: bool counted; int word_counter; ifstream infile; ofstream outfile; vector<string> ignored_words; }; char quit(); void menu_option_error(); int main() { FileReader myFileReader; myFileReader.load_ignore_file(); char choice = 0; do{ myFileReader.reset(); switch(myFileReader.menu()) { case 1: myFileReader.add_ignore(); break; case 2: myFileReader.file_count(); break; default: menu_option_error(); } }while(choice!='Y'); return 0; } //FUNCTION DEFINITIONS int FileReader::menu() { int choice; system("clear"); cout << endl << endl << endl << endl << setw(40) << " MENU \n" << setw(40) << " ------------------------ \n" << setw(40) << " \n" << setw(40) << " 1. Add an ignore word \n" << setw(40) << " \n" << setw(40) << " 2. Perform File Count \n" << setw(40) << " \n" << setw(40) << " 3. Quit \n" << setw(40) << " \n" << setw(40) << " ------------------------ \n"; if(counted) {cout << setw(40) << "| Word Count = " << word_counter << setw(76) << "|\n"<< setw(40) << "------------------------";} cout << endl << endl << endl << endl << "Enter choice: "; cin >> choice; cin.ignore(80,'\n'); return choice; } void menu_option_error() { cout << "\n\n\t\aT'was not a valid menu option! (Try Again)"; cout << "\n\n\tPress [ENTER] to continue..."; cin.get(); } char quit() { char yes_no; cout << "\n\n\tAre you sure you want to quit? (Y/N): "; cin >> yes_no; cin.ignore(80,'\n'); system("clear"); cout << endl << endl; return toupper(yes_no); } void FileReader::reset() { bool counted = false; word_counter = 0; } void FileReader::load_ignore_file() { string temp; infile.clear(); infile.open("ignore.dat"); if(infile.fail()) return; while(!infile.eof()) { while(getline(infile, temp)); ignored_words.push_back(temp); } infile.close(); } void FileReader::add_ignore() { string temp; outfile.open("ignore.dat",ios::app); if(outfile.fail()) { cout << "\n\n\t\aOUTPUT FILE WRITE FAIL!" << "\n\t(Program will now terminate)" << "\n\n\tPress [ENTER] to Continue..."; cin.get(); exit(1); } cout << "\n\n\n\tEnter word to be ignored: "; cin >> temp; temp += ' '; outfile << temp; outfile.close(); } void FileReader::file_count() { bool match; string temp; int back = ignored_words.size(); cout << "\n\n\tExample: C:\\myfile.txt" << "\n\t-------" << "\n\n\tEnter the path to your file to count: "; cin >> temp; infile.clear(); infile.open(temp.c_str(),ios::in); if(infile.fail()) { cout << "\n\n\t\aCannot Locate File -OR- File Does Not Exist!" << "\n\n\tPress [ENTER] to continue..."; cin.ignore(); cin.get(); return; } infile.clear(); while(!infile.eof()) { infile >> temp; match = false; for(int c=0; !match && c<back; c++) if(temp == ignored_words[c]) match = true; if(!match) word_counter++; } infile.close(); counted = true; }



LinkBack URL
About LinkBacks


