Hi Salem,
Thanks for the code, the line;
Code:
finx.seekg(0, ios::beg);
Made the while loop only read three strings, I counted with cout.
And this is what your function returned with no alterations to your function;
Code:
Enter your sentence, end it with a period: zzzy.
Input sentence to program, from input.txt;
zzzy
Matching chars: 5, Program response;
zzyy 1
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Press any key to continue . . .
And the result if I removed the line;
Code:
finx.seekg(0, ios::beg);
Code:
Enter your sentence, end it with a period: zzzy.
Input sentence to program, from input.txt;
zzzy
Matching chars: 5, Program response;
zzyy 1
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Press any key to continue . . .
So I changed your function a bit and here is the new function;
Code:
void findMatch(){
std::ifstream fin1("inputSentenceentence.txt");
if (!fin1) { std::cerr<<"file error\n"; std::exit(1); }
string inputSentence;
getline( fin1, inputSentence );
fin1.close();
std::ifstream finx("holdDatabase.txt");
if (!finx) { std::cerr<<"file error\n"; std::exit(1); }
const int maxMatches = 7;
char matchList[] = "1234567";
string dataBaseSentences[maxMatches];
int charTotals[maxMatches] = { 0 };
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
for ( int nMatch = 0 ; nMatch < maxMatches ; nMatch++ ) {
finx.clear(); // clear any errors such as EOF
string database;
while( getline( finx, database ) ){
int matching_char_one = 0;
int matching_char_two = 0;
int matching_char_three = 0;
int matching_char_four = 0;
int matching_char_five = 0;
int matching_char_six = 0;
int matching_char_seven = 0;
int char_one = 0;
int char_two = 0;
int char_three = 0;
int char_four = 0;
int char_five = 0;
int char_six = 0;
int char_seven = 0;
for (string::size_type a = 0; a < database.length(); ++a) {
if ((database[a] == matchList[0]) && (one == 0)) {
char_one++;
break;
}
if ((database[a] == matchList[1]) && (two == 0)) {
char_two++;
break;
}
if ((database[a] == matchList[2]) && (three == 0)) {
char_three++;
break;
}
if ((database[a] == matchList[3]) && (four == 0)) {
char_four++;
break;
}
if ((database[a] == matchList[4]) && (five == 0)) {
char_five++;
break;
}
if ((database[a] == matchList[5]) && (six == 0)) {
char_six++;
break;
}
if ((database[a] == matchList[6]) && (seven == 0)) {
char_seven++;
break;
}
}
for (string::size_type b = 0; b < inputSentence.length(); ++b) {
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_one == 1) && (one == 0)) {
matching_char_one++;
}
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_two == 1) && (two == 0)) {
matching_char_two++;
}
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_three == 1) && (three == 0)) {
matching_char_three++;
}
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_four == 1) && (four == 0)) {
matching_char_four++;
}
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_five == 1) && (five == 0)) {
matching_char_five++;
}
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_six == 1) && (six == 0)) {
matching_char_six++;
}
if ((database.find(inputSentence[b]) != string::npos) && (inputSentence[b] != ' ') && (char_seven == 1) && (seven == 0)) {
matching_char_seven++;
}
}
if((matching_char_one > 2) && (char_one == 1) && (one == 0)){
dataBaseSentences[0] = database;
charTotals[0] = matching_char_one;
one++;
break;
}
if((matching_char_two > 2) && (char_two == 1) && (two == 0)){
dataBaseSentences[1] = database;
charTotals[1] = matching_char_two;
two++;
break;
}
if((matching_char_three > 2) && (char_three == 1) && (three == 0)){
dataBaseSentences[2] = database;
charTotals[2] = matching_char_three;
three++;
break;
}
if((matching_char_four > 2) && (char_four == 1) && (four == 0)){
dataBaseSentences[3] = database;
charTotals[3] = matching_char_four;
four++;
break;
}
if((matching_char_five > 2) && (char_five == 1) && (five == 0)){
dataBaseSentences[4] = database;
charTotals[4] = matching_char_five;
five++;
break;
}
if((matching_char_six > 2) && (char_six == 1) && (six == 0)){
dataBaseSentences[5] = database;
charTotals[5] = matching_char_six;
six++;
break;
}
if((matching_char_seven > 2) && (char_seven == 1) && (seven == 0)){
dataBaseSentences[6] = database;
charTotals[6] = matching_char_seven;
seven++;
break;
}
}
}
finx.close();
for ( int nMatch = 0 ; nMatch < maxMatches ; nMatch++ ) {
cout << endl << endl
<< "Matching chars: " << charTotals[nMatch]
<< ", Program response; " << endl
<< dataBaseSentences[nMatch];
}
}
And the results the new function returns;
Code:
Enter your sentence, end it with a period: zzzy.
Input sentence to program, from input.txt;
zzzy
Matching chars: 5, Program response;
zzyy 1
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Matching chars: 4, Program response;
yyyzz 4
Matching chars: 3, Program response;
zzzzz 5
Matching chars: 0, Program response;
Matching chars: 0, Program response;
Press any key to continue . . .
These results match the function I previously posted before your reply.
So I hope my alterations to the function is OK and now I can worry about outputting only the string with the highest number of matching chars? And tips on how I would do that? And thank you very much for your contribution to my program, thats a genuine statement, thank you.