Thread: Help using strcpy for arrays

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Question Help using strcpy for arrays

    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]);
    }

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Code:
     strcpy(listOfWords[randomChoice], selectedWord[numOfWords]);
    in the function SelectWord, you're asking strcpy to treat selectedWord as though it were an element within an array of strings. If selectedWord is meant to be the output and listOfWords is the source, then your backwards. Assuming other bits or okay, you *should* be able to clean up the error using.

    Code:
     strcpy(selectedWord, listOfWords[randomChoice]);
    If selectedWord is the string that you'd like to copy into listOfWords[randomChoice], then

    Code:
     strcpy(listOfWords[randomChoice], selectedWord);
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    On a side note, strcpy() is evil due to being susceptible to a buffer overflow which could make your program vulnerable. Use strncpy() instead. This doesn't really matter for a program made for "educational" reasons but it would matter for a commercially made program. Point being, it is best to establish good C programming habits from the beginning even when you don't care about the security aspects.

    Welcome to the board!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Euph11 needs to say whether they're learning C or C++.

    Because at the moment, the code is that 'train-wreck waiting to happen' known as C/C++. A horrible mix of random things borrowed from each language.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Thanks for the help. ronin helped me fixed the problem and C_ntua also provided some useful information. Salem...I'm in the same boat you are from looking at my code. What I am being taught has not once been distinguished from C++ to C. I had been led to believe it was C++ all along but from reading things from the textbook and forums, it seems there are multiple things coming from C also. I agree with what you're saying, I have a feeling this is something I will have to learn for myself because the professor is not distinguishing the difference.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays, strcpy and strcmp Problems
    By Windu102 in forum C Programming
    Replies: 3
    Last Post: 08-23-2008, 01:00 AM
  2. Help with strcpy and arrays
    By doyle96 in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2007, 03:35 PM
  3. Using strcpy() and arrays of structs
    By vital101 in forum C Programming
    Replies: 3
    Last Post: 04-26-2007, 09:04 PM
  4. strcpy error with arrays
    By trang in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 10:13 PM
  5. help with strcpy and arrays with header file
    By Agnesa in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 05:06 PM