Well this is my first attempt at receiving help from the community. I am currently a Computer Science major at a University and am in my second semester. My current teacher has not been of much help outside of the class so I am hoping this community can.
Before the haters come at me, this is an assignment, but I am not asking for the assignment to be done for me. I am merely asking a question about an error I'm receiving. I want to understand what's going wrong, not have it done for me.
I am attempting to assign "randomChoice" as the array index of the "listOfWords" array then copy "listOfWords[randomChoice] to "selectedWord". I am not sure if it is that I do not fully understand the use of strcopy() or something wrong with my arrays. Hopefully you guys can help!
This is my error:
Code:
hangman.cc: In function ‘void SelectWord(char (*)[80], int, char*):
hangman.cc:84: error: invalid conversion from 'char' to 'const char*'
And my code:
Code:
#include <iostream>#include <fstream>
#include <cstdlib>
#include <cassert>
#include <cstring>
using namespace std;
//Global constants
const int MAX_WORDS = 80;
const int MAX_USED = 80;
//Function Declarations
void ReadWords (char listOfWords[][MAX_USED], int numOfWords, ifstream& inFile);
void SelectWord (char listOfWords[MAX_WORDS][MAX_USED], int numOfWords, char selectedWord[MAX_WORDS]);
//Call the functions to use in the program
int main (int argc, char *argv[])
{
char listOfWords[MAX_WORDS][MAX_USED];
int numOfWords;
char selectedWord[MAX_WORDS];
ifstream inFile;
srand(time(0));
inFile.open(argv[1]); //Open file
assert(inFile); //Check if file opened correctly
//Function Calls
ReadWords (listOfWords, numOfWords, inFile);
SelectWord (listOfWords, numOfWords, selectedWord);
}
//Function will read in words from data file into a 2D array
void ReadWords (char listOfWords[MAX_WORDS][MAX_USED], int numOfWords, ifstream& inFile)
{
numOfWords = 0;
while ((inFile.peek() != EOF) && (numOfWords < MAX_WORDS))
{
inFile.getline (listOfWords[numOfWords], 100, '\n');
numOfWords++;
}
//Debugging - Array content check
/*
for(int i = 0; i < numOfWords; i++)
{
cout << listOfWords[i] << endl;
}
*/
}
void SelectWord (char listOfWords[MAX_WORDS][MAX_USED], int numOfWords, char selectedWord[MAX_WORDS])
{
int randomChoice;
randomChoice = rand()%numOfWords;
char i;
//Debugging - Generating random number check
//cout << randomChoice << endl;
strcpy(listOfWords[randomChoice], selectedWord[numOfWords]);
}