I intend to find out if any permutation(s) of a user inputted string is a valid word in a text file with few words.
After inputting string, nothing happens! What’s wrong with “if” stmt or what? Also, if I write an else that is executed which means control never reached to if even though I input words present in the list.txt
What can I try to fix this?
//check if any permutation of a user inputted word are in a pre defined text file
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(){
cout<<"Enter a word to check for the presence of any
<< of its permutation in a file \n";
string word;
cin>>word;
sort(word.begin(), word.end());
vector<string> str;
do str.push_back(word);
while( next_permutation(word.begin(),word.end()) );
ifstream readFile("list.txt");
string line;
while(readFile>>line){
for (int i = 0; i < str.size(); ++i){
if(line==str[i]){
cout << "found " << str[i] << endl;
break;
}
}
}
system("pause");
return EXIT_SUCCESS;
}
1) You should store the strings to search in a vector.
Then you can loop through them like this
2) To compare your strings just use ==
You might need to remove leading and trailing white space from
linefirst though.