Issues passing an array to a function

Hello, I'm trying to write a program that will solve wordsearches. Right now, I'm running into a problem where I cannot pass an array as a parameter, even though it seems like I've followed the tutorial perfectly. (http://cplusplus.com/doc/tutorial/arrays/)

The specific error I'm getting is:

prog.cpp: In function ‘void print_word_search(char*)’:
prog.cpp:15: warning: array subscript has type ‘char’
prog.cpp:15: error: invalid types ‘char[char]’ for array subscript
prog.cpp: In function ‘int main()’:
prog.cpp:34: warning: array subscript has type ‘char’
prog.cpp:34: warning: array subscript has type ‘char’
prog.cpp:39: error: cannot convert ‘char (*)[5][5]’ to ‘char*’ for argument ‘1’ to ‘void print_word_search(char*)’


Here is my source 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
#include <iostream>
#include <string>

#define HEIGHT 5
#define WIDTH 5

using namespace std;


void print_word_search(char w_search[]){

	for (char x = 0; x < WIDTH; x++){
	
		for(char y = 0; y < HEIGHT; y++){
			cout << w_search[x][y];
			}
			
		cout << endl;
		}
	}
	

int main(){

	char word_search [WIDTH] [HEIGHT];
	char x, y;
	
	cout << "Please enter each letter in the word search.  Sorry for the tedium :(" << endl;
	
	//input array
	for (x = 0; x < WIDTH; x++){
		
		for (y = 0; y < HEIGHT; y++){
			cin >> word_search[x][y];
			}
			
		}
		
	print_word_search(&word_search);
		
	return 0;
	}
Try this and see if it works.
1
2
3
4
5
6
for (char x = 0; x < WIDTH; x++){
		for(char y = 0; y < HEIGHT; y++){
			cout << w_search<<x<<y<<endl;
			}
		cout << endl;
		}

print_word_search(&word_search[WIDTH][HEIGHT]);. I hope all best and good luck.
Strange...I made the changes you asked, but when I ran it on IDEone, there wasn't any output; not even "Please enter each letter"...as if "cout" was broken. Note that it was still taking input, so it wasn't a compile error. However, when I removed "print_word_search(&word_search[WIDTH][HEIGHT]);" it did give me output. When I tried to run it on codepad.org, it just flat out gave me a compile error. Unfortunately, I'm not at home right now, so I don't have access to a "real" compiler at the moment, so I'll have to get back to you with the results of that later.

Anyhow, I trust that you made the correct changes. Can you please explain what changes you made and why you made them? That will help me learn :P

1
2
3
4
5
6
7
for (char x = 0; x < WIDTH; x++){
		for(char y = 0; y < HEIGHT; y++){
			cout << w_search<<x<<y<<endl;
//output the variable for x & y.
			}
		cout << endl;
		}

print_word_search(&word_search[WIDTH][HEIGHT]);//this calls the functions. You can also have [x][y], as you said char word_search[WIDTH][HEIGHT] you would have to the same for print_word_search(&word_search) as you are not calling nothing.
The output I got when I wrote Facebook,Google,Youtube and Yahoo was into a different font or somewhat. You should try and download visual studio it would be much better for you. I try it on xCode and it came up with
Please enter each letter in the word search. Sorry for the tedium:(
but not with the other stuff I got with visual studio after entering all the same word. Sorry about the explain.
Last edited on
Topic archived. No new replies allowed.