Simple coding problems part 1

This is my code. I am to have the user choose a number of random words and have the program create those words. The words meaningless but the problem is getting the code to accept WordArray when I pass it through the function. I am using Anjuta on Ubuntu (using G++). The code looks clean to me. This is not all of it just the important part
the errors i receive are below the 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<cstring>

#include<stdlib.h>

using namespace std;


//RANDOM WORD GENERATOR DEFINITION
void randword(char* arrayone[],int num){int len = 3;
	for(int i = 0 ; i < num ; i++){
	//DETERMINES THE LENGTH OF THE WORD
		len += rand() % 4;
			for(int j = 0; j<= len ; j++){
				arrayone[i][j] = rand()% 26 + 'a';
					if (j == len)
						arrayone[i][j] ='\0';
				}
			}
}
//PRINT ARRAY FUNCTION DEFINITION 
void PrintArray(char* Array[], int num)

{ for( int i = 0; i < num ; i++)

	cout << Array[i]<< endl;

}

//MAIN FUNCTION DEFINITIONS
int main(){ int wordnum;
	cout << "How many words do you want to generate?" ;
	cin >> wordnum;
char WordArray[wordnum][7];
char SortArray[wordnum][7];
	randword(WordArray, wordnum);
	cout <<"\n\n Thank you. \n Here are your words.\n"<<endl;
PrintArray(WordArray,wordnum);
	
	return 0;	
	
}


error: cannot convert 'char(*)[7] to char** for argument '1' to void randword(char**, int)
error: cannot convert 'char(*)[7] to char** for argument '1' to void PrintArray(char**, int)

If I knew how to resolve it I wouldn't be here so I am kinda at you alls mercy.
This is unrelated to your problem, but you can't put a variable inside the brackets that are defining a stack array (as in lines 35 and 36). You'll need to use dynamic allocation.
Change the code to this:
1
2
3
char **WordArray=new char*[wordnum]
for (int a=0;a<wordnum;a++)
    WordArray[a]=new char[7]; //Or whatever length you want. 

Then change the type of the first parameter of both functions to 'char **'.
That's true of certain compilers (namely some microsoft compilers). GCC will
allow you to create a variable sized array as he is doing just fine.

You need to change randword and PrintArray's declarations to:

1
2
void randword( char arrayone[][7], int num );
void PrintArray( char Array[][7], int num );

Huh. You're right. I didn't know that.
Topic archived. No new replies allowed.