If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

But it doesn't work correctly, so my doubt is: how could I do it so that the number of attempts subtracts one number when I fail a letter and the game ends when you run out of attempts?
(I don't know if I've explained well, any question ask me)

Important: I have to complete the exercise just with the structure it appears in the code I've done, only with the library <iostream>.

Re: Simple hangman game

Important: I have to complete the exercise just with the structure it appears in the code I've done, only with the library <iostream>.

You can't 'only use <iostream>'. You also either need to use properly c-style null-terminated char arrays (and include <cstdio> and <cstring>) or use c++ style strings (and include <string>). For c++, as suggested by Paul in post #2 it is better to use c++ style strings. c-style gets() is a 'dangerous' function that can cause buffer overflow and should not be used.

Also,

Code:

if(palabra[j]==a)

palabra is not defined. Should it be word?

Code:

i = 0;
while(word[i] != '\0')
i++;

This attempts to obtain the number of characters in the c-style word char array. If you are going to use c-style char arrays, why not use the strlen(..) function?

But it doesn't work correctly, so my doubt is: how could I do it so that the number of attempts subtracts one number when I fail a letter and the game ends when you run out of attempts?

You need to have a loop (while?) around the code that asks for a letter, checks it against the sentence and outputs the string. Each time round the loop check the number of attempts and if exceeded or the whole word guessed then exit the loop and print an appropriate message.

Last edited by 2kaud; June 6th, 2013 at 05:38 PM.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Simple hangman game

Important: I have to complete the exercise just with the structure it appears in the code I've done, only with the library <iostream>.

Having looked at your code again, it is possible to just use the <iostream. library but I'd advise against it as you are re-inventing what has already been done, learning bad habits and not using c++ the way you should!

You can't use the c++ string class - as you can't include <string> so you are stuck with c-style char arrays. For the purpose of this program, when you define a char array you should set its maximum length like this

Code:

const int MAXLEN = 256;
char word[MAXLEN] = {0};

Then instead of using gets(word), you will need to use cin.getline(word, MAXLEN) and instead of a = getchar() you use a = cin.get() (or cin.get(a)). As you are using iostream for input, you should check the stream for errors after use. You will also need to use your loop to count the number of chars as you can't use strlen() as you can't include <cstring>!

Hope this helps.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Re: Simple hangman game

I only have to do it with the library <iostream> because my teacher doesn't allow me to do it in a different way.
2kaud, I can't use cin.getline, I have to do it with the structure it appears in the code, not a different type of code, just using for, else, while, gets... I don't know if I'm explained well.

Re: Simple hangman game

Can't I use gets and getchar only with iostream?? My teacher has taught me that way... My compilor is Dev-C++, if it's necessary.
With the structure I mean to write the code the simplest way as possible, nothing difficult, following the structure of the code..
Sorry 2kaud, I'm a beginner and I'm lost with this. Could you help me to do this exercise, to see how should I do it nex times?

Re: Simple hangman game

It doesn't matter what your teacher taught you. To properly use gets(), the <cstdio> header must be included. Those are the rules of C++.

My compiler is Dev-C++, if it's necessary.

1) This forum is for Visual C++, not any other compiler. You want the Non-Visual C++ forum for the questions you're asking.

2) Dev-C++ is not a compiler. It is an Integrated Development Environment. The actual compiler is gcc / g++. Also, Dev-C++ uses a very old and outdated version of g++ (3.x). The g++ compiler has been at version 4.x for several years now.

With the structure I mean to write the code the simplest way as possible, nothing difficult, following the structure of the code..

Using std::string does not change the "structure" of your code. What it does is make your code safer, so that you are not continuing to make the mistakes you have now.

How does using getline() and std::string instead of gets() and char arrays change the structure of your code? It doesn't.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.