Program doesn't run correctly....

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!
Line 26: The variables pattern,text,from_pos, mismatch do not exist. They have not been declared in scope anywhere so you cannot use these.

Your confusing variable declaration with parameters to a function. When you protoype (declare) a function you tell it what types of variables your going to let it accept. This doesn't create any variables.

You need to declare the variables before your able to send them through to a function.

e.g

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
#include <iostream>

using namespace std;

void someFunction(int A, int B, int C);
// A, B and C do not exist. The function will accept 3 Ints.

int main() {
 int var1 = 0;
 int var2 = 1;
 int var3 = 5;

 someFunction(var1, var2, var3);
 // A, B and C do not exist in this scope

 return 0;
}


void someFunction(int A, int B, int C) {
 cout << "A = " << A << endl;
 cout << "B = " << B << endl;
 cout << "C = " << C << endl;

 // var1, var2 and var3 do not exist in this scope.
}

No, the variables are globally declared in lines 6 and 7.

The trouble is with the variable mismatch because it seems to be already reserved. Try to change it to another name like mmatch...

But:
- the function inexact_pos() is not declared correct. In line 5 you use const char and in line 29 just char (the better choice)
- i would recommend to use unsigned int instead of unsigned
- it can cause troubles comparing signed and unsigned integers (line 39 and 51), why not use signed integers?
- i can't see, where you use the variable x declared in line 30
- inexact_pos has no return statment!

And there is another point in your program that don't make any sense to me. Please think about it:
The only use of the function shall_we_continue is to wait for a key, that is not 'n'. Why don't you put this into your main function? - And why is it called two times? First to run the program (probably), but another time just to determine the return value of the program?
On shall_we_continue(): This function seems very poorly designed at best. I think perhaps you were trying to make a loop to continue the program, but it does not work as you intended. Your program starts, and the user (with no prompting or display of any data) is required to know they need to enter a single character that is not 'n' in order to run the program. And should they not, they have to type 'n' twice (assuming cin does not screw up because of remnant '\n' chars) because main() calls it twice.

On inexact_pos(): This function just cins a long line of variables; the user is expected to know exactly what order to input them and what type they are (what if the user inputs more then 18 characters?), apparently. cin may also fail here because of remnant '\n' chars but I'm not perfectly sure as I wouldn't try to do something like that in any case...

I suppose what my point is that you have absolutely no user interface. The only thing you seem to print out is the results of your program; and that is assuming the user can even get that far without even a display like "enter a character/number" etc.
Topic archived. No new replies allowed.