Hi;
I have a number of text strings in an access db file. I want to compare those text strings with the user input. I need to make sure that atleast 60% of the text input matches with the correct string to declare it a correct answer. e.g.
for a string in access db "I would love to do that", if a user inputs something like " I love to do that" it should display "correct' message.
For simple comparison in MFC, I used

void CT2I::Correct(CString s)
{
	CString str;
	KillTimer(0);
	m_ans.GetWindowTextA(str);
	if(str == correct) // correct is acces db field with correct string)
	{
		score++;
		MessageBox("CORRECT");
		UpdateData(FALSE);
		CString s;
		s.Format("%d",score);		
		m_score.SetWindowTextA(s);
			
	}
	else
		if(str != correct)
	{
		MessageBox("Wrong Input! Click 'OK' for Correct Answer",0);
		m_ans.SetWindowTextA(correct);
						
	}

Now I want to use CStringArray and want to assign two different lists for 'str' and 'correct' respectively but I am not able to work out with stricmp availible options. Something like this

CStringArray matchList;
CStringArray matchList1;

matchList.Add(str);
matchList1.Add(correct);
 if (stricmp(matchList, matchList1) <0)
{
score++;
MessageBox("CORRECT");

}
else
{
..
...
}

Any suggestions/advices are welcome.

Recommended Answers

All 8 Replies

what anout a function that will break up both strings (the one from the input and the one from the access db) into string arrays and compare them in a double function, constituted by nested for that will go through both arrays, counting the amount of matching words, loops that will return the results from the comparison... something like this:

String 1: "I would love to do that"
String 2: "I love to do that"

Array 1:
| "I" | "would" | "love" | "to "| "do" | "that" |

Array 2:
| "I" | "love" | "to" | "do" | "that" |

Total words in array 1: 6
Amount of matching words: 5

Percetnage of matching words= 5/6 * 100 = 83.33%

83.33 > 60 ? True

printf("true")

@ Nichito
you mean somethng like this..

CWordArray MyWord;
CWordArray UserWord;
MyWord.SetSize(100);
UserWord.SetSize(100);
MyWord.Add(correct);
UserWord.Add(str);

for (int j=0;j<UserWord.GetSize();j++) 

              {  	for(int k=0;k<MyWord.GetSize();k++)
		{
		
		if (_stricmp((char*)&UserWord,(char*)&MyWord) >0)
	
	{	
		
		score++;
		MessageBox("CORRECT");
                 }
                               else
                                       {
                                       }
                               }
             }

Still waiting for some suggestions. Ran out of ideas, whatever I tried it's not working. It simply compares the strings and not the contents that are being passed to the string. I think need some pointers here. :(

Do something like this :

//mainSrc is the source where the secondarySrc will compare from
//function returns the percentage of matching words
float checkList(const  std::vector<string>& mainSrc, const std::vector<string> secondarySrc){
  size_t matchingWords = 0;
  //for each word in secondarySrc, check if mainSrc has the secondarySrc[i]. If so then increment matchingWords, else move forward
  //return matchingWords/mainSrc.size();
}

Do something like this :

//mainSrc is the source where the secondarySrc will compare from
//function returns the percentage of matching words
float checkList(const  std::vector<string>& mainSrc, const std::vector<string> secondarySrc){
  size_t matchingWords = 0;
  //for each word in secondarySrc, check if mainSrc has the secondarySrc[i]. If so then increment matchingWords, else move forward
  //return matchingWords/mainSrc.size();
}

@ firstPerson

thanks for your suggestion..
I did like this.. but in this way, every space is also counted as character and plays role in comparison whereas I would like to compare only words. e.g. if 3 words out of 5 are correct, it should reply correct.

float strcm(CString str1, CString str2)
{
	char *cstr1= new char [1024];
	char *cstr2= new char [1024];
	
	strcpy(cstr1, LPCTSTR (str1)); 
	strcpy(cstr2, LPCTSTR (str2));
	
	size_t len1=strlen(cstr1), len2=strlen(cstr2);
	
	if(len2==0)
	
	{
	MessageBox("Enter some text to compare");
	return -1;
	}
	else
	{
	
	float lenCmp;
	unsigned i,j, *previous, *next;
	previous=(unsigned *)calloc( len1+1, sizeof(unsigned));
	next=(unsigned *)calloc( len1+1, sizeof(unsigned));
	for(i=0; i<len2; ++i)
	{
		for(j=1; j<=len1; ++j)
			if( cstr1[j-1] == cstr2[i])
				next[j]=previous[j-1]+1;
			else next[j]=previous[j]>=next[j-1]?previous[j]:next[j-1];
		swap( &previous, &next);
	}
	lenCmp=(float)previous[len1];
	free(previous);
	free(next);
	return lenCmp/=len1;
	}

}

float result=strcm(correct, str);
if (result==0 || result>=0.6) //for 60% match
	
{
		MessageBox("CORRECT");
}

else
{
}

Any more suggestions are welcome.

@ firstPerson

thanks for your suggestion..
I did like this.. but in this way, every space is also counted as character and plays role in comparison whereas I would like to compare only words. e.g. if 3 words out of 5 are correct, it should reply correct.

----
Any more suggestions are welcome.

I assumed when you made the above comment about SPACEs, you corrected the code to ignore SPACEs. What other suggestions do you need?

@WaltP
Using the mentioned code, I can compare string but I was asking a way to compare words.. what I need is array of words with some id/index to each word. e.g. I gave user a line ..

" I .. in love .... you"

and ask him to fill the blanks where the blank words are 'am' and 'with'. I have the correct string in some database, which I want to match with user input word by word.
In mentioned code I used CString and char array whereas I think I can use CWordArray and linked list for word comparison.
What to do you say?

I don't understand the problem. If you have the word in a database, and they type in the word, it seems a simple matter to compare them. What are you leaving out of your description?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.