Here is what the program is supposed to do:
User enters string "pattern", as well as string "text".
Additionally, he also enters an int "from_pos" and "mismatch".
The program compares the letters of "pattern" with the letters of "text", and it starts from char location "from_pos" in "text" (the second string), with a margin of error of "mismatch" (meaning how many letters off it can be from actually matching "pattern", the previous word).
If, the program does find the first string contained in the second string, then the program will print out the starting char's location (the initial char location of the second string that makes a match with the first string).
That in short, is what the program is supposed to do.
I wrote it all out (I am almost sure that I wrote a very accurate algorithm in the code), but, like many times before, I get some repeating errors.
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
using namespace std;
int shall_we_continue();
int inexact_pos(const char pattern[],const char text[],unsigned from_pos,unsigned mismatch);
char pattern[19], text[19];
unsigned from_pos,mismatch;
int main()
{
shall_we_continue(); //Calls function shall_we_continue
return shall_we_continue();
}
int shall_we_continue()//In this function, program determines whether it will continue or not
{
char t;
cin >> t;
if(t=='n')
return 0;
else
return inexact_pos(pattern,text,from_pos, mismatch);
}
int inexact_pos(char pattern[19], char text[19],unsigned from_pos,unsigned mismatch)
{ int x;
cin >> pattern >> text >> from_pos >> mismatch; //User enters input
if(strlen(text)<strlen(pattern))//If the second string is shorter than the first,
{cout << "no match\n"; } //then program prints out "no match"
else //Otherwise, program will continue
{ int count=from_pos;
int ret_val=strlen(text);
for(int i=from_pos,k=0; i<strlen(text)-from_pos; i++)//A loop to compare the chars
{ //from the first string with the second
if(pattern[k]==text[i])
{ k++;
count++;
}
else
{ k=0;
count=0;
}
if(count==(strlen(text)-mismatch))//If there is a match, then program will
{ //print out it's starting char location
cout << (ret_val-count+1);
}
}
}
}
|
And here are the errors/warnings that I get back from my compiler (Visual Studio C++ 2005):
1 2 3 4 5 6 7 8 9
|
c:\documents and settings\root\my documents\visual studio 2005\projects\ex7b\ex7b\ex7b.cpp(26) : error C2872: 'mismatch' : ambiguous symbol
1> could be 'c:\documents and settings\root\my documents\visual studio 2005\projects\ex7b\ex7b\ex7b.cpp(7) : unsigned int mismatch'
1> or 'mismatch'
1>c:\documents and settings\root\my documents\visual studio 2005\projects\ex7b\ex7b\ex7b.cpp(38) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data
1>c:\documents and settings\root\my documents\visual studio 2005\projects\ex7b\ex7b\ex7b.cpp(39) : warning C4018: '<' : signed/unsigned mismatch
1>Build log was saved at "file://c:\Documents and Settings\Root\My Documents\Visual Studio 2005\Projects\ex7b\ex7b\Debug\BuildLog.htm"
1>ex7b - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
|
If I would be able to get some help as to what I am doing wrong, I would really appreciate that.
Thanks!