Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0

    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:

    #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
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Important: I have to complete the exercise just with the structure it appears in the code I've done, only with the library <iostream>.
    Then why are you using gets()? This dangerous C function is not part of the iostream class. Also never use a function that doesn't limit the number of characters it will accept into a C-string.

    You may want to start by using braces {} to surround all the statements that should be in your control statements. Without the braces only one line will be part of your control statements.

    And please use code tags when posting code.

    Jim
  4. #3
  5. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    As a newcomer, you need to be made aware that the forum software does not by default retain indentation. This applies to quoted text as well. Therefore, instead of quote tags, you need to put code samples in between [code] tags, like so:

    [code]
    code goes here.
    [/code]

    You can do this automatically with the '#' button at the top of the editing window, or the highlight marker button right next to it.

    As a courtesy to you as a new member, I have re-posted your code with [highlight] tags; you can view and copy the plain source by clicking on the double chevron button at the top right of the viewing window.

    C++ Code:
    #include <iostream>
    #include <string>
    using namespace std;
     
    int main()
    {
        char a, intro;
        string word;
        string sol;
        int i, j, attempts=0;
     
        cout << "Introduce a sentence:" << endl;
        getline(cin, 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';
     
        cin >> a;
     
        cout << "Introduce a letter: ";
        cin >> a;
     
        cin >> intro;
        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;
     
        return 0;
    }


    Note that I fixed (in the sense that it now compiles) a number of errors, though I didn't alter the logic of the program any. First off, I added a return statement to the end of the main() function. Second, I changed word and sol to string objects, as you had declared them as zero-length char arrays previously, which wouldn't work. Third, I replaced all references to the C-style I/O with iostreams. HTH.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in ShortUnderstanding the C/C++ Preprocessor
    Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    Then why are you using gets()? This dangerous C function is not part of the iostream class. Also never use a function that doesn't limit the number of characters it will accept into a C-string.

    You may want to start by using braces {} to surround all the statements that should be in your control statements. Without the braces only one line will be part of your control statements.

    And please use code tags when posting code.

    Jim
    I use gets because my teacher has taught me that way. I have to do it the same way he does, so I can not afford another type of structure :(


    Note that I fixed (in the sense that it now compiles) a number of errors, though I didn't alter the logic of the program any. First off, I added a return statement to the end of the main() function. Second, I changed word and sol to string objects, as you had declared them as zero-length char arrays previously, which wouldn't work. Third, I replaced all references to the C-style I/O with iostreams. HTH.
    Sorry Schol-R-LEA! But I can't use the library #include <string>, by the same reason I wrote up, I only have to use <iostream>, but I have no idea how to do it :confused:

    Hope you can help me

    Here's again the first code:

    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;
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    I only have to use <iostream>
    The gets() function is not part of iostream. You will need to include the <cstdio> header file to use that extremely dangerous function. I suggest you look into using getline() instead, which happens to work with the iostream class.

    Forget you ever heard about gets()!

    You also need to fix your C-strings. You can't create a C-string without specifying a size.

    Jim
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    You will need to change universities.
    If transfer is out of the question, do not write a letter supporting your computer science teacher's tenure.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Another question we should ask is what compiler he's using.

    If he's using Turbo C++ or Borland C++, neither of which supported the new 1998 standard, then he will not have the basic string class available to him. That would explain why he is restricted to using C-style strings -- all the new teaching materials that I've seen won't even tell you about C-style strings, let along stdio functions.

    We've seen that compiler used a lot in Southern Asia, especially in India, but I don't know about the extent of its use in Latin American schools.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    I doubt that the OP is using Turbo-C++ since he is using the <isostream> header file which is standard C++. So the string class should be available, although it seems outlawed by the instructor. The OP has already stated that he can't use the <cstdio> functions, although he is in fact using a C-stdio function. He stated he can only use <iostream> several times.

    Jim
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    The compiler I'm using is Dev-C++, the only one allowed by the teacher.
    I'm not in the university, I'm in the high school and this is my final project, so it's bugging me a lot :(
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Indent your code! Using code tags without bothering to indent your code is worse than useless.

    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;
    }
    You tried to run that? Why? It doesn't compile cleanly! You get warnings, which means that the code is broken!

    Here is what I get when I compile it with MinGW gcc, which is the same compiler that you're using because it's the same compiler that comes with Dec-C++:
    C:\otros\dcw>g++ -Wall gabby01.cpp
    gabby01.cpp: In function `int main()':
    gabby01.cpp:13: implicit declaration of function `int gets(...)'
    gabby01.cpp:35: implicit declaration of function `int getchar(...)'
    gabby01.cpp:44: `palabra' undeclared (first use this function)
    gabby01.cpp:44: (Each undeclared identifier is reported only once
    gabby01.cpp:44: for each function it appears in.)

    C:\otros\dcw>
    Your program has no idea what gets nor getchar are, nor what palabra is supposed to be. That confuses the compiler, which is why it issues warnings. Then it makes assumptions about the code that it should generate. Believe me, the last thing you want is a confused compiler making assumptions. Who knows what that code might do? Certainly not what you expected it to.

    Never ignore warnings! And never run a program that issues warnings. Warnings are much more important than error messages could ever be. Never ignore warnings! I know that based on more than 20 years professional experience with C and C++, during which I have seen code go bad in ways you could never imagine. Never ignore warnings!
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    Soorry, I've fixed the code and now it works in Dev-C++. Here it is:
    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;
    }

    So, could you help me to get this works, the number of attempsts?
  22. #12
  23. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Originally Posted by Gaggy
    Soorry, I've fixed the code and now it works in Dev-C++.
    No, it does not work! You're still getting warnings!

    C:\otros\dcw>g++ -Wall gabby02.cpp
    gabby02.cpp: In function `int main()':
    gabby02.cpp:13: implicit declaration of function `int gets(...)'
    gabby02.cpp:35: implicit declaration of function `int getchar(...)'

    C:\otros\dcw>
    Warnings mean that you're doing something wrong! Never ignore warnings!

    And before you complain that I'm using a different compiler, I'm not. MinGW gcc is the compiler that Dev-C++ uses. I got my copy of MinGW gcc when I installed Dev-C++. Even though I immediately abandoned Dev-C++ as worse than useless, I still use MinGW gcc for test programs and small projects. So I am using the same compiler as you are and it says that your program is broken and should not be run.

    The compiler has absolutely no idea what gets and getchar are supposed to be, because you won't tell it! You've already been told by jimblumberg what you need to do to fix that.

    Plus you are not returning a value at the end of main. I have no idea why the compiler isn't complaining about that.

    And even though you gave the sol array a size, you did not give word a size. As it is, if you write anything to word then you are clobbering the memory around it, which would either destroy the contents of other variables or could even cause the program to crash. Your program is broken!

    Also, you initialize an empty string thus:
    Code:
        char sol[256] = "";
    And what do you expect attempts to do? You never decrement it, nor do you use it to control anything. In fact, your program only performs a single pass to the end:
    Code:
        i=0;
        while(word[\i]!='\0')
            i++;
    
        cout<<"You have "<<attempts<<" attempts"<<endl;
    When control reaches the while statement, it increments i until i equals the number of characters in word. Why do you even have it in there? And then after that while loop is completed, you cout the number of attempts once and only once. Just as the rest of the program is executed once and only once.

    What are you trying to do? It might help you to add in your comments so that you can be clear to yourself what you are trying to do and so that we can have some clue to work with.
    Last edited by dwise1_aol; June 7th, 2013 at 12:12 PM.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    12
    Rep Power
    0
    dwise1_aol, I can't see these warnings you are showing. I'm using the version 4.9.9.2 of Dev-C++, if it's necessary.
    I'll try to be clear about what the program should do:

    Now the game says: -Introduce a sentence- and then -Introduce the attempts number- I introduce it and it's all right. Then it says: -Introduce a letter-, I introduce it and it shows me where the letter was hidden. It's OK until now, where the program should say again: -Introduce a letter-, but it doesn't do it and when I introduce a letter again the program closes.
    I'd like the program said: -Introduce a letter- until the number of attempts run out if I don't guess the words or I win the game, guessing all the words.

    I hope you'll undertand me well. I'm mad about this program.
  26. #14
  27. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    As I said, Dev-C++ is almost worse than useless. There should be an option somewhere to turn warnings on, though I seem to recall having read that that doesn't work.

    I go directly to the command line and run MinGW gcc from the command line as it was intended to be run. That bypasses Dev-C++ and allows me to specify the -Wall option which displays all warnings. If you know how to use the command line and can navigate to your program's directory, then I would recommend that approach.


    I want you to go back in and start commenting the program itself. You will need to do that in order to turn it in anyway. That way, you can show both us and yourself what each part of the code should do and where it's supposed to go back to repeat itself.

    You expect the program to somehow loop back to where it says "Introduce a letter: ". OK, so show us exactly where that is supposed to happen. That will also make the problem clear to you. A common problem for programmers is that we "know" what the program is doing, so when we try to read it we see what we think is there rather than what we actually wrote. That is when a second pair of eyes becomes important, but that second pair of eyes needs an explanation to work with. That is what comments will provide. Commenting your code is a very important step.
    Last edited by dwise1_aol; June 7th, 2013 at 12:46 PM.
  28. #15
  29. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    You've stated that you can only use the <iostream> library for the I/O, correct? Yet you are using getchar() and gets(), both of which are declared in the <cstdio> header. When asked about this contradiction before, you stated that you were following what you were taught, but the problem is that the code shouldn't compile correctly without the <cstdio> header included in the program. Do you see the problem here?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in ShortUnderstanding the C/C++ Preprocessor
    Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo