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
#include<iostream>
#include<cstring>
#include<stdlib.h>
usingnamespace 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=newchar*[wordnum]
for (int a=0;a<wordnum;a++)
WordArray[a]=newchar[7]; //Or whatever length you want.
Then change the type of the first parameter of both functions to 'char **'.