Storing an user-written string in an array/vector

Greetings.

I'm trying to code a basic "hangman" game.
(If you don't know what it is, it's a two player game where one thinks of a secret word, and the second must guess it by asking whether a character is within the word, or not)

These are the two things bringing me problems:
1) Code::Blocks tells me there is a type incompatibility when I want to split the word in letters and store one in each position of a vector. What am I doing wrong? How can I fix it?

2) As I don't know how long will the word be, I'm trying to learn how to use dynamic vectors. Am I doing something wrong?


Keep in mind I'm very, very new at C++


Here's the code bit, separated for an easy read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int wordCodification (string player1, string &wordToGuess)
{
unsigned int i,amountCharsInWord;

cout<<"Input word to guess, "<<player1<<endl;

cin>>wordToGuess;

amountCharsInWord=strlen(wordToGuess);         //error in this line

char convToArray[amountCharsInWord];

strncpy(convToArray, wordToGuess.c_str(), sizeof(convToArray));

convToArray[sizeof(convToArray) - 1] = 0;

for (i=0;i<=(amountCharsInWord-1);i++)
    {cout<<convToArray[i]<<endl;};

cout<<"if out of loop, write this"<<endl;
return 0;
}



(I know this must've been asked before; I just couldn't find an open topic where to post it)
What are you trying to do in this function?
I'm not sure what you're trying to do by attempting to convert a your std::string to a C string (char array), but you probably don't need to do that.
If you want to access the individual characters of a std::string, you can do it just like with any normal array:
1
2
for (unsigned i = 0; i < wordToGuess.size(); ++i)
    // Do something with wordToGuess[i] 

By the way, the error on line 9 comes from the fact that strlen only works on C strings -- for std::string, use the .size() member function (see my code snippet above).

For your second question, std::string already dynamically resizes itself based on how long the word is, so you don't need to worry about dynamic arrays or anything like that.
Last edited on
Hey, thanks for the help.
Removing c_str fixed it (I had copied it from an example someone gave me...).

What I want to do is this
User inputs a word -> the word is stored on a string, and the string converted into a vector. -> The other user is asked to write one letter at a time; if the letter is within the first user's vector, then it's copied on a second vector.

The second vector, of the same length, contains symbols. The symbols are replaced every time the second user guesses a letter from the first user's vector, and displayed on screen.

Let's play a theorical match:
My secret word is "house". House is stored into a vector, one char per space.
Onscreen, you get *****.
You input 'o'.
Screen shows *o****.
You input 'r'.
Nothing happens.
You input 'e'.
The screen shows *o**e.

The working mechanic is more or less clear?

What my bit of code tries to do is to split the word in chars, store it in a vector, and create a second vector of the same size of the first.

EDIT: if there's a more efficient way of doing it I'm open to suggestions.
Last edited on
Hmm, you don't need vectors to do that.
Everything you said can be done using just std::strings.

It would be something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::string word, outputWord;
// ... (word = the secret word, outputWord = the starred version)
char input;
// ... (Get input from user)
unsigned pos = word.find(input);
if (pos != std::string::npos) // If we found the character
{
    // User guessed correctly
    outputWord[pos] = word[pos];
    // You'll have to replace all of the other occurrences as well -- use a loop for that
    // Hint: word.find(ch, p) will find the first occurrence of 'ch' at or after position 'p'
}
else
    // User guessed incorrectly 
Wonderful! I've done it from scratch using your suggestion, and it works excellently! Thank you!

I've a long way to go and a lot to read.

If you don't mind me asking though... just out of curiosity sake, would it be too hard to do it the way I thought of doing it first?

And how is line 6 meant to be read?
If the position of the letter in the word is different from... what? I thought that npos is used as "within the entire string" or "with (length) as maximum value". Why is it "different than" instead of "equal to"?

And I don't get how the IDE knows what string am I referring to when you just write "string::npos".

I don't question if it works because it clearly does! I just can't tell what's happening...

Again, thanks :)
I don't think it would have necessarily been harder to do it the way you had thought of (using std::vectors) -- it would have just a bit more redundant, perhaps.
(But do try it out! If anything, it's good practice with std::vectors :) .)

word.find(input) returns the position of the first occurrence of the character input in the string word, or std::string::npos if that character was not found.
Hence, by checking if (pos != std::string::npos), we're essentially checking whether or not the user's input was found.
(Here, you can think of npos as meaning "not found" or "the specified character is located at position 'no position' (npos)".)

You can write std::string::npos instead of referring to a specific std::string because npos is, in a sense, "shared" by all std::string objects (it's a static member constant).

It is possible to write something like
1
2
3
std::string hi = "Hello";
if (hi.find('e') != hi.npos) // Access 'npos' through 'hi'
    // ... 
but I haven't seen anyone that actually does this.
Last edited on
Topic archived. No new replies allowed.