Problem with definitions

This is a simple program that I wrote only to learn how the simple parts of
the program syntax (meaning I wrote it only to see what I do wrong in general):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;


int inexact_pos( char pattern[19],  char text[19], unsigned from_pos, unsigned mismatch);


int main()
{
	
	inexact_pos(pattern[],text[],from_pos,mismatch);
	
	return 0;
}



int inexact_pos( char pattern[19],  char text[19], unsigned from_pos, unsigned mismatch)
{
	cin >> pattern >> text >> from_pos >> mismatch;

	return 0;
}


And, I get these errors:

1
2
c:\documents and settings\root\my documents\visual studio 2005\projects\ex7b\ex7b\ex7b.cpp(11) : error C2065: 'pattern' : undeclared identifier
c:\documents and settings\root\my documents\visual studio 2005\projects\ex7b\ex7b\ex7b.cpp(11) : error C2059: syntax error : ']'


What exactly am I doing wrong? I don't understand those errors....
What is the best way for me to write my program?

If you'd be able to explain how and why I would really appreciate that, thanks!
Last edited on
My first piece of advice is to avoid using character arrays. These are a very much C construct and should be replaced by strings in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;

int inexact_pos(string pattern, string text, unsigned from_pos, unsigned mismatch);

int main() {

 string myPattern = "bob";
 inexact_pos(myPattern, "some text with bob in it", 0, 0);

 return 0;
}

int inexact_pos(string pattern, string text, unsigned from_pos, unsigned mismatch) {

 // do stuff;
 return 0;
}
The problem is, is that I need to use character arrays, because I need them for my homework. This particular question is not my homework (my homework questions are a lot more complicated), I just posted this one up so I can understand what I am doing wrong from the syntax aspect.

What is the way that I can continue writing the program as I did in a more correct fashion?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

void someFunction(char array1[], char array2[]);

int main() {

  char arrayOne[20] = {0};
  char arrayTwo[20] = "Some Array Two";

  sprintf(arrayOne, "Some Value");

  someFunction(arrayOne, arrayTwo);

  return 0;
}

void someFunction(char array1[], char array2[]) {
  cout << "array1: " << array1 << endl;
  cout << "array2: " << array2 << endl;
}
You can`t use identifiers pattern, text, from_pos, mismatch definition without in function main().
But, when you define them in function main(), you can use it.

sory for my bad English
Topic archived. No new replies allowed.