CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2013
    Posts
    3

    Simple hangman game

    Greetings to everyone!
    Firstly, I must apolagize for my English, I'm learning.
    What I have to do is the hangman game the simplest way as possible.
    This is the code I have at the moment:



    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    char a,intro;
    char word[]={};
    char sol[]={};
    int i,j,attempts=0;
    
    cout<<"Introduce a sentence:"<<endl;
    gets(word);
    cout<<"Introduce the attempts number"<<endl;
    cin>>attempts;
    for(i=0;i<25;i++)
      cout<<endl;
    
    
    i=0;
    while(word[i]!='\0')
    i++;
    
    cout<<"You have "<<attempts<<" attempts"<<endl;
    
    cout<<"You have "<<i<<" letters"<<endl;
    for(j=0;j<i;j++)
    {
      sol[j]='*';
      cout<<"*";
    }
    cout<<endl;
    sol[j]='\0';
    
    a=getchar();
    
    
    cout<<"Introduce a letter: ";
    a=getchar();
    
    
    intro=getchar();
    for(j=0;j<i;j++)
      if(palabra[j]==a)
       sol[j]=a;
    
    cout<<endl;
    for(j=0;j<i;j++)
     cout<<sol[j];
    cout<<endl;
    
    cin>>a;
    }



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

    ¿Could you help me? Thank you so much!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simple hangman game

    You should be using string class such as std::string, and not what you're doing now. For example, this is totally wrong:
    Code:
    int main()
    {
      char word[]={};  // what is this supposed to do?
      char sol[]={};// what is this supposed to do?
    
      gets(word);  // what magic is going to dynamically size a dumb array?
    //..
    http://www.cplusplus.com/reference/cstdio/gets/

    The array must already hold enough characters to fit your input. The gets() knows nothing about resizing arrays to fit the data.
    I have to complete the exercise just with the structure it appears in the code I've done
    What do you mean by "structure"? The code has bugs, just as I described.

    Here is the way to get string input using C++ streams and using dynamic strings.
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      std::string word;
      std::string sol;
      //...
      getline(cin, word);  
    }
    only with the library <iostream>.
    Even this is wrong. The proper includes for gets() is <cstdio>, but nowhere did you include it.

    Regards,

    Paul McKenzie

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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?

    http://www.cplusplus.com/reference/cstring/strlen/

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2013
    Posts
    3

    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.

    Here's the code again, I've fixed some bugs:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char a,intro;
        char word[]={};
        char sol[256]={};
        int i,j,attempts=0;
    
        cout<<"Introduce a sentence:"<<endl;
        gets(word);
        cout<<"Introduce the attempts number"<<endl;
        cin>>attempts;
        for(i=0;i<25;i++)
            cout<<endl;
    
    
        i=0;
        while(word[i]!='\0')
            i++;
    
        cout<<"You have "<<attempts<<" attempts"<<endl;
    
        cout<<"You have "<<i<<" letters"<<endl;
        for(j=0;j<i;j++)
        {
            sol[j]='*';
            cout<<"*";
        }
        cout<<endl;
        sol[j]='\0';
    
        a=getchar();
    
    
        cout<<"Introduce a letter: ";
        a=getchar();
    
    
        intro=getchar();
        for(j=0;j<i;j++)
            if(word[j]==a)
                sol[j]=a;
    
        cout<<endl;
        for(j=0;j<i;j++)
            cout<<sol[j];
        cout<<endl;
    
    
        cin>>a;
    }

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Simple hangman game

    You haven't read or understood the points raised by Paul in post #2 as they are still present in your code

    Code:
    char word[]={};
    No! This is not how you correctly define a char array.

    Code:
    gets(word);
    ..
    a=getchar();
    No! gets() and getchar() require the header <cstdio> which you say you can't use. gets() should never be used anyhow as it can lead to serious buffer overflow problems.

    You haven't yet included the loop to repeatly ask the user to input letters to try to find the word(s) as suggested in my previous post.

    I have to do it with the structure it appears in the code
    What structure?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jun 2013
    Posts
    3

    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?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Simple hangman game

    Quote Originally Posted by Gaggy View Post
    Can't I use gets and getchar only with iostream??
    No.
    My teacher has taught me that way...
    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.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured