   filling a char array with letters
  I make a "Guess the word" game. I have an empty array: "letters_guessed" in which I store the letters correctly guessed out of the (secret) word (kinda like hangman). This code:for (int i=1; i<position; i++)//position is the index of a correctly guessed //letter inside the original word{strcpy((char*)letters_guessed, "_ ");}gives a run-time error and exits. Is this way of typecasting- char to char*- correct?
  Can you post more code? We need to know exactly what 'letters_guessed' is, among other things.   ;  for (int i=1; i<position; i++){    letters_guessed='_ ';}like this?;  You can't just cast a char to char*. If you have a char, and need its address, use the &-operator.char c;char* p = &cYour code would becomefor (int i=1; i<position; i++){    strcpy(&(letters_guessed), "_ ");}But I'm not sure that'll do what you would want it to do.E.g. why do you start at position 1 ? Is there a space you want to preserve at position 0? And why fill the entire array up to position? Are you sure you want the terminating zero to be copied also (which is what strcpy will do)? Etc.Could you tell us what your loop is supposed to do?   ;  Nope, that's not correct. You don't have to use strcopy, you could just do this:(I don't thing you have to use a loop...)char *letters_guessed = new char[32];// Some code here// Check if the guessed letter is okif (guessedLetter)letters_guessed[guessedPosition] = '_';// More code here// Clean up when you're done.delete[] letters_guessed;[EDIT]wow... this forum is fast... my post is addressed to the topic starter :)   ;  To SamLowry (and all): Thanks, it solved the errors. The loop has to do this: suppose the computer thought of word "player". If someone guesses "a" (correct, itis in "player"), the loop writes "_" for all the letters not guessed yet, then a piece of code writes the "a" at its correct place (using strstr()), then another loop writes "_"'s till it reaches the word's strlen() (END OF WORD).What you see in the game:(word: Player)Guess! a  Correct!So far you've guessed:_ _ a _ _ _  (ie "player")Just like Hangman.   ;  But if I guess y now, won't that erase the a?_ _ a _ _ _becomes_ _ _ y _ _Also, beware of the terminating 0 strcpy copies each time; for now it works because it is overwritten each time and only the last survives, which correctly ends the string. More concretely, your loop works as follows:_ 0_ _ 0_ _ _ 0I'm just pointing it out since I think you'll bump into this later on.   ;Quote:Original post by asdqweTo SamLowry (and all): Thanks, it solved the errors. The loop has to do this: suppose the computer thought of word "player". If someone guesses "a" (correct, itis in "player"), the loop writes "_" for all the letters not guessed yet, then a piece of code writes the "a" at its correct place (using strstr()), then another loop writes "_"'s till it reaches the word's strlen() (END OF WORD).What you see in the game:(word: Player)Guess! a  Correct!So far you've guessed:_ _ a _ _ _  (ie "player")Just like Hangman.This can be done much more efficiently using standard strings instead of char arrays (since you can treat each string like an array anyway).   ;  To ju2wheels: Sadly, I can't use strings because they can't be uppercased or lowercased (_strupr() only accepts char*).   ;Quote:Original post by asdqweTo ju2wheels: Sadly, I can't use strings because they can't be uppercased or lowercased (_strupr() only accepts char*).For the most part it's safe to assume that if something can be done with raw character arrays, it can also be done (and probably more safely and with more flexibility) using std::string, the C++ standard library in general, and perhaps Boost. This definitely applies for case manipulation of strings.If you're not sure how to do something in particular, just ask, but in any case if your intent is to learn C++, you should go ahead and switch to std::string.   