Hello, I'm somewhat new to C++. I am taking a class on it in college, but the teacher doesnt like to explain things very well. Basically her idea of helping us learn C++ is this:
"this is how you start all your programs in this class:
#include <iostream>
using namespace std;
int main()
{
}
"
She never explainted even what "#include <iostream>" or "using namespace std;" ment. So, this brings us to my question.
I'm trying to write a c++ program *for a project in class* that is a hang man game. The hangman is stored in a character array. The user is able to input a word into a 25 character array "char word[25];". There are a couple of things I need to do.
the first thing is, I need to know how to count how many characters are stored in my character array. SO, I can clear the screen and then print out underscores for how many characters there are in the variable "word". So, if player one enters the word "apple" for player two to guess then it will see that "hey theres 5 characters in the array" then it will clear that and then print out 5 underscores like this "_ _ _ _ _". Then, when the user types in a letter, it will then, if false, make a limb fall off, and if its true then it will replace the underscore with the letter guessed. SO, since were using apple as the example, lets say, the user guesses "l". It will the turn "_ _ _ _ _" into "_ _ _ l _". then say the user guesses "a". then it will turn "_ _ _ l _" into "a _ _ l _" so on and so fourth. My teacher requires us to do this project and we haven't gone over how to do any of this kind of stuff. I mean, I know variables, if statements, arrays and some other stuff, but I'm stuck.

hangman project
MattFletcher
From writing this program I learned about using "#include <string.h>" and "#include <stdio.h>" as headers. Also I learned about using "strlen()" and "strspn()". It was fun to write, and very frustrating when things would work, but learned better ways of doing some things, and other ways of doing things that I was origionally taught. Thank you all for your help, I apreciate it.
logan_india
David N.4117
To count the number of characters in a string, you can use strlen() included from cstring. The "#include" preprocessor directive lets you include header files, which will give you access to the functions/classes/etc. they provide. So to use strlen, you must put "#include <cstring>" at the top of your program, which will give access to several useful functions that work on c-style strings ( http://www.cplusplus.com/reference/clibrary/cstring/ ). Note that C strings must be NULL terminated ( with the NULL '\0' character ) for strlen to work ( the compiler NULL terminates a string for you when you do something like "char str[] = "string";", though ). Or, if you wanted to impress your teacher, and you know about loops, you could implement your own strlen() type function. Just iterate through the string until you find the '\0' character, and return the number of iterations it took to reach it'.
Krimboss
connect2sandeep
"Or, if you wanted to impress your teacher, and you know about loops, you could implement your own strlen() type function."
It may impress the teacher, but it won't impress the boss.
Use strlen().
p0lar_bear
Shawnzee
for(int f = 0; f < wordlength; f++)
{
if(guess1 == word[f])
{
wrong = 'n';
}
}
if(wrong == 'y')
{
for(int o = 0; o < 18; o++)
{
hungman[t]
}
t++;
}
but yea, I went to my teacher saying "I got the hang man project done" waiting for her to be suprised that I had it done early and waiting to show her so she'd be more suprised but she then said "oh, that was a mistake, you werent actually supposed to do it" so all this work for nothing. I almost smaked her lol.
I do however want to improve it, so if anyone is willing to take a look at my code and tell me ways to improve apon what IM doing that would be great.
Felipe Heidrich
dev_.net
You should get a book to learn from. It will require too much time for people here to explain everything, and there are many execellent books and tutorials that already explain things very well.
There are two ways that you can use strings in a C++ program. The old C way uses character arrays such as "char word[25];". The C++ way uses a "string" class that is in the std namespace; that is what "using namespace std;" is for. There is much more in the std namespace. The strlen function is part of the C Runtime Library and works with C-style strings. For C++ strings, you can use the length member function. It is better to learn and use the C++ way of doing things; it is easier and you don't really need to learn the C-style way until later.Douglas H. Troy
One thing that might be educational is to write a specification for the game. That might not be necessary since a rules of a game are like a specification. Getting accustomed to writing specifications however can help you many times in the future.
I can't promise to look at your code but you can use my email address in my profile if you wish.