How can I determine the number of letters in a word?

I'm currently attempting to make a hangman game.
The game will be a computer vs a person.
I would like the program to be able to look through a list of words, and pick one randomly to use. I know that I can accomplish this using streams, and that it shouldn't be too difficult. However, the next step is the part that confuses me. After I get the word into my program, I want to put it into an array, but I need to know how many letters the words contains, in order to create the correctly sized array. Additionally, I need to be able to put all of the letters into the correct spaces, so I'm really confused about how to do that. I need to know because for the next part, I want my program to cout the correct number of blank spaces, depending on the size of the word. I'm sorry if this is a dumb question, but how can I create this part of my program. All help is greatly appreciated!
closed account (4Gb4jE8b)
I don't usually do this but i'm just gonna give you the answer.

1
2
3
4
	for(int i = 0; i<length;i++)
	{
		char a = content[i];
	}

define int i outside the loop, and i will be the number of letters in your string. if you want to access specific letters individually, make the char into an array or just use the same string and specify the number of the address of the letter you want to use.


for why i gave you the answer go here: http://www.cplusplus.com/forum/beginner/34497/
Last edited on
Use C++ strings. They're dynamic in length, they have a method to tell you their size, and they're so easy to use! Array of words? EASY! Use a vector! Its a dynamic array!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <string>
// Other includes

int main()
{
   std::vector < std::string > words; // Simple and less dangerous than regular arrays.
   // code to read or get words from file
   // code to choose a word and copy it to a single lone string
   // code to make a hidden string of the same length as the real word
   // code to ask player for move
   // code for computer to guess
   // code to show word and guesses left to player/computer
}


There's an outline.
closed account (4Gb4jE8b)
oh yeah... i forgot... there's this function called .length() for strings... it returns how many characters there are... hahaha

But really all a string is is an array of characters. You shouldn't have to create a separate array for it. you can even get specific characters by doing stringname[address]
you can even get specific characters by doing stringname[address]

You mean stringname[index].
closed account (4Gb4jE8b)
indeed i do, it was one am my time, my apologies :P
Topic archived. No new replies allowed.