The text in file_in.txt;Code:#include <iostream> #include <string> #include <string.h> #include <fstream> #include <vector> #include <cstdlib> // exit() #include <cctype> // isalpha(), tolower() using namespace std; void readByString(std::string f1, std::string f2, std::string f3){ int count = 0; if(f1 == f2) count++; if(count > 0) cout << f1 << " " << f2 << endl << f3 << endl; } int main() { std::ifstream fin("file_in.txt"); std::ofstream fout("file_out.txt"); std::ifstream fin2("file_in_2.txt"); std::ofstream fout2("file_out_2.txt"); if (!fin || !fout ||!fin2 || !fout2) { std::cerr<<"file error\n"; std::exit(1); } string temp3; while( getline( fin2, temp3 ) ){ float c_; bool word = false; char letters[] = "abcdefghijklmnopqrstuvwxyz"; char *this_letter; while ((c_ = fin.get()) != EOF) { if (c_ == '\'') continue; // skip apostrophe if (std::isalpha(c_)) { if ((this_letter = strchr(letters, tolower(c_))) != NULL){ fout << this_letter[0]; } word = true; } else if (word) { fout<<'\n'; word = false; } } fout.close(); fin.close(); float c_2; bool word2 = false; char letters2[] = "abcdefghijklmnopqrstuvwxyz"; char *this_letter2; while ((c_2 = fin2.get()) != EOF) { if (c_2 == '\'') continue; // skip apostrophe if (std::isalpha(c_2)) { if ((this_letter2 = strchr(letters2, tolower(c_2))) != NULL){ fout2 << this_letter2[0]; } word2 = true; } else if (word2) { fout2<<'\n'; word2 = false; } } fout2.close(); fin2.close(); ifstream ifs ("file_out.txt"); ifstream ifs2 ("file_out_2.txt"); std::ifstream ifs3("file_in_2.txt"); if (!ifs || !ifs2) { std::cerr<<"file error\n"; std::exit(1); } int j = 0; vector<string> text_file; string temp; vector<string> text_file2; string temp2; vector<string> text_file4; string temp4; if(ifs.is_open()){ while( getline( ifs3, temp4 ) ){ while( getline( ifs, temp ), getline( ifs2, temp2 ), getline( ifs3, temp4 ) ){ for (j = 0; temp[j] != '\0'; j++) temp[j] = tolower(temp[j]); readByString(temp, temp2, temp4); text_file.push_back( temp ); text_file2.push_back( temp2 ); } text_file4.push_back( temp4 ); } ifs.close(); ifs2.close(); ifs3.close(); } } }
zeethe price is right
The text in file_in_2.txt;
a blue budgie
zeethe price is alright
Here are the console results;
zeethe zeethe
zeethe price is alright
price price
is is
Process returned 0 (0x0) execution time : 0.125 s
Press any key to continue.
___________________
I want to test for more than one word. So in the readByString function I want to test if (count > 1), but when I use 1 instead of 0 the results fail.
Right now it looks for the word "zeethe", but I want to find "price" too.
I have been busy googling and trying to do this for about a day now, please help me.



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.