m1sterb0b

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.


Re: Visual C++ Language hangman project

thrice

 

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'.





Re: Visual C++ Language hangman project

Simple Samples

m1sterb0b wrote:
She never explainted even what "#include <iostream>" or "using namespace std;" ment. So, this brings us to my question.
I sure would not teach that way.

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.

m1sterb0b wrote:
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 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.

m1sterb0b wrote:
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".
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.






Re: Visual C++ Language hangman project

Brian Kramer

"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().





Re: Visual C++ Language hangman project

m1sterb0b

I ended up using strlen(), and I also used strspn(). I used strlen() to get the origional character length of the first word then I used strspn to get the length of the guessed word to keep comparing the two against each other. basically I took the variable word[25] which stores the word, then guess[25] and then I did i = strlen(guess, word) That measures to see how many characters match up. So then I did at the bottom of the do loop *at the while part of the loop* "}while((i != wordlength) && (wrong == 'n'));" wrong of course being the amounts of times they could get it wrong. basically every time they got one wrong it would go to an if statement. nested in a for loop:

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]Time = hangman[t]Time;

}

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.




Re: Visual C++ Language hangman project

Simple Samples

m1sterb0b wrote:
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.
So were you not supposed to do it at all or were you supposed to do it another way Either way, it sounds as if your teacher can improve communication. I don't want to jump to conclusions without hearing both sides and it does not concern me but if that is what happened then I would be frustrated. It is unfortunate that your teacher at least says that you have done good.

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.






Re: Visual C++ Language hangman project

Brian Kramer

Best way to learn to code is practice... practice... practice! Computer science classes can only contribute to a very small fraction of the learning curve, so consider your Hangman work a win!



Re: Visual C++ Language hangman project

m1sterb0b

I do consider it a win. I learned more making that project than I have in class. As for the reason that she wouldnt accept it. Apparently she accidently forgot to take that requirement out of the mid term website that shows the requirements. Then when i did it and gave it to her, shesl ike "oh, yea, I forgot to take that out of there, you werent supposed to do it." When I asked for al ittle extra credit she refused that. She wouldnt even run it and she insisted that I didnt actually write the code. She came to that conclusion without looking at my code. She wouldnt take the project at all. So yea, she didnt believe I wrote it, she didnt want to look at it, and wasnt willing to give me any credit at all, IM quite mad about it. Now i Have to do the actual mid term which is a stupid "rate doritio's program".

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.




Re: Visual C++ Language hangman project

Brian Kramer

Quite an odd situtation.  At least you can make a comment about it in the end-of-semester instructor review.



Re: Visual C++ Language hangman project

Peter Ritchie

Brian Kramer wrote:

"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().

...It won't impress the boss once you're hired; but, it's often an interview question: "write strlen".




Re: Visual C++ Language hangman project

Simple Samples

m1sterb0b wrote:
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.
Those are the C way of doing things. One possibility is to learn to do things the C++ way. That can be a very useful education.