Hello guys, Ron here...im going to start my IT lessons next year but I already started learning some basics in C++...actually, I tried making a Hangman game already but there's something wrong...My loop is incorrect...please help me...so here is my first ever code..

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{char name[20],Lvl1[4]="ENVY",letter;
 int trial=3,pts=0;

cout<< "  -------+++++++-------+++++++-------+++++++-------+++++++-------+++++++-------";
cout<< "\n";
cout<<"\t\t  --------------------------------------------\n";
cout<<"\t\t  | #  #   #   #   #  #### #   #   #   #   # |\n";
cout<<"\t\t  | #  #  # #  ##  # #     ## ##  # #  ##  # |\n";
cout<<"\t\t  | #### ##### # # # #  ## # # # ##### # # # |\n";
cout<<"\t\t  | #  # #   # #  ## #   # #   # #   # #  ## |\n";
cout<<"\t\t  | #  # #   # #   #  ###  #   # #   # #   # |\n";
cout<<"\t\t  --------------------------------------------\n\n";
cout<< "  -------+++++++-------+++++++-------+++++++-------+++++++-------+++++++-------";
cout<< "\n";
cout<< "\nAbout the game: Hangman is a game that will let you play with the";
cout<< "\ncomputer in which it will generate word/words to be guessed by the";
cout<< "\nplayer by entering letters one at a time. If the player guesses a";
cout<< "\nright letter,it will be placed in its right position.\n";
cout<< "\nGuess with less than 4 mistakes, then you win the game";
cout<< "\nOtherwise, you lose.";
cout<< "\nPlease Fill this up first\n";
cout<< "\nPlayer Name:";
gets(name);
clrscr();
cout<< "Lets begin!";
cout<< "\nPlayer Name:"<<name;
cout<< "\n\t\t\t\t===================";
cout<< "\n\t\t\t\tWelcome to Level 1!";
cout<< "\n\t\t\t\t===================";
cout<< "\nHint: This is another term used to refer to someone who's jealous- _ _ _ _";
cout<< "\nPoints Earned:"<<pts;
cout<< "\nPlease enter a letter:";
cin>>letter;

    do
    {
        for(int pos=0;pos<4;pos++)
            if(letter==Lvl1[0])
                {cout<< "Nice guess!";
                cout<< "\nYou earned 10 points!";
                pts=pts+10;
                gotoxy(68,6);
                cout<<Lvl1[0];}
            else if(letter==Lvl1[1])
                {cout<< "Nice guess!";
                cout<< "\nYou earned 10 points!";
                pts=pts+10;
                gotoxy(70,6);
                cout<<Lvl1[1];}
            else if(letter==Lvl1[2])
                {cout<< "Nice guess!";
                cout<< "\nYou earned 10 points!";
                pts=pts+10;
                gotoxy(72,6);
                cout<<Lvl1[3];}
            else if(letter==Lvl1[3])
                {cout<< "Nice guess!";
                cout<< "\nYou earned 10 points!";
                pts=pts+10;
                gotoxy(74,6);
                cout<<Lvl1[3];}
            else
                {cout<< "Oops!That letter is not part of the word!";
                trial=trial-1;
                cout<< "You have "<<trial<<" tries left";}
            if(trial==0)
                cout<< "You are dead, GAME OVER";
                break;
        }
    while(pts<30);
        \\this is where the player gets to the next level,i plan to use goto statement\\
    }

Recommended Answers

All 9 Replies

Could you wrap the code between

tags it's hard to read the code without proper indentation

Could you wrap the code between

tags it's hard to read the code without proper indentation

AHm sorry..I can't understand what youre trying to say..wrap?sorry, i dont know these terms yet sir....?for what is it? please tell me so next time I can post what you want me to do..

Put the word code enclosed in square brackets before the first line of code you post to this board and the word code preceeded by a backslash enclosed in square brackets after the last line of code you post to this board. This will preserve the indentation you (hopefully) use when writing your code and make it much easier to read.

You actually have several loops in your code as well as several goto statements. goto statements should be avoided whenever possible as they make code difficult to read and debug.

Please try to describe what isn't happening that you think should be happening and why you think it has to do with one (or more) of the loops.

Hello guys, Ron here...im going to start my IT lessons next year but I already started learning some basics in C++...actually, I tried making a Hangman game already but there's something wrong...My loop is incorrect...please help me...so here is my first ever code..


I can't help you with your code but I got and advice for you if I may, this code is way to large to learn something from it. You should start with some very short and basic codes to understand first and then learn c++, just copying large codes without knowing what parts of the program do is not really gonna help.

I'm back with my new and improved code..haha...the only problem now is that i can't seem to input a letter after I have guessed the word.(Note: After guessing the word you should type "proceed" to go to the next level.)About the [CODE] stuff, Is this the way?(I thank you for the replies)

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{char name[20],Lvl1[4]="ENVY",letter;
 int trial=3,pts=0,x=0;

cout<< "  -------+++++++-------+++++++-------+++++++-------+++++++-------+++++++-------";
cout<< "\n";
cout<<"\t\t  --------------------------------------------\n";
cout<<"\t\t  | #  #   #   #   #  #### #   #   #   #   # |\n";
cout<<"\t\t  | #  #  # #  ##  # #     ## ##  # #  ##  # |\n";
cout<<"\t\t  | #### ##### # # # #  ## # # # ##### # # # |\n";
cout<<"\t\t  | #  # #   # #  ## #   # #   # #   # #  ## |\n";
cout<<"\t\t  | #  # #   # #   #  ###  #   # #   # #   # |\n";
cout<<"\t\t  --------------------------------------------\n\n";
cout<< "  -------+++++++-------+++++++-------+++++++-------+++++++-------+++++++-------";
cout<< "\n";
cout<< "\nAbout the game: Hangman is a game that will let you play with the";
cout<< "\ncomputer in which it will generate word/words to be guessed by the";
cout<< "\nplayer by entering letters one at a time. If the player guesses a";
cout<< "\nright letter,it will be placed in its right position.\n";
cout<< "\nGuess with less than 4 mistakes, then you win the game";
cout<< "\nOtherwise, you lose. Note: All letters should UPPERCASE";
cout<< "\nPlease Fill this up first\n";
cout<< "\nPlayer Name:";
gets(name);
clrscr();
cout<< "Lets begin!";
cout<< "\nPlayer Name:"<<name;
cout<< "\n\t\t\t\t===================";
cout<< "\n\t\t\t\tWelcome to Level 1!";
cout<< "\n\t\t\t\t===================";
cout<< "\nHint: This is another term used to refer to someone who's jealous- _ _ _ _";
A:
cout<< "\nPlease enter a letter:";
cin>>letter;
        for(int pos=0;pos<4;pos++){
            if(letter==Lvl1[pos]){
                x=x+2;
                gotoxy((66+x),6);
                cout<<Lvl1[pos];
                pts=pts+10;
                cout<< "\nYou earned 10 gold points!";
                cout<< "\nPoints Earned:"<<pts;
                cout<< "\nPlease Enter another guess:";
                cin>>letter;
                if(pts==40)
                {cout<< "You guessed all the letters!";
                clrscr();
                goto B;}
                }
        }
            if(letter!=Lvl1[4])
                    {cout<< "Oops!That letter is not part of the word!";
                    trial--;
                    cout<< "\nYou have "<<trial<<" tries left";
                    }
                    if(trial==0)
                        cout<< "\nYou're Dead, GAME OVER!";
                    else
                        goto A;


B:
cout<< "Lets begin!";
cout<< "\nPlayer Name:"<<name;
cout<< "\n\t\t\t\t===================";
cout<< "\n\t\t\t\tWelcome to Level 2!";
cout<< "\n\t\t\t\t===================";
cout<< "\nHint: The name of the moon -_ _ _ _ _ _ _";
cout<< "\nPlease enter a letter:";
cin>>letter;
}

Oh yea about the basic codes..I think I understand most of it...Just excited to try and make my first code...When you mean basics do you mean this?

#include<iostream.h>
void main()
{char name[15];

cout<< "Please Enter your Name";
cin>>name;
cout<<name;

}

1) main should have return type int, not void. Even if your compiler implementation allows use of void, don't do it, if for no other reason than int is one less char than void so it's less typing.

2) char Lvl1[4]="ENVY";

That should cause an error as you need to leave space for a null terminating char in Lvll. It should be either:
char Lvll[5] = "ENVY"; or
char * Lvll = "ENVY";

3)if(letter!=Lvl1[4])

Lvll[4] doesn't exist has you have declared it. If you declare Lvll as I suggest then Lvll[4] will be a null char when "ENVY" is in Lvll.

4) put lines 47 and 48 between line 53 and 54

5)(Note: After guessing the word you should type "proceed" to go to the next level.)

Then you will have to write code to do it.

6) declare pos on line 9, not line 40

7) if(letter!=Lvl1[4])

Change that to: if(pos == 4)

8) only problem now is that i can't seem to input a letter after I have guessed the word.

Do you mean that you see the output on lines 68 to 74 but can't put anything into letter on line 75? If so how do you know if there is anything in letter after line 75 if you don't evaluate the value of letter after line 75?

9) I can't figure out what use x in in your program?

At least I think all of that is correct. It's difficult to say with absolute certainty since I haven't written your program into my compiler and debugged it.

I would suggest tackling simpler things... I'm pretty new to the language, but I've written hangman in C++ and it's much cleaner. I'm not trying to say I'm better than you by ANY means, but what I AM saying is that it doesn't really look like you're ready to tackle something like this. There are some tricks like creating functions to take in the player's guess, check if the guess was partially right, making a container for the words, etc... I'm no pro, but I think you should try something else and come back to this later.

Wow thanks guys...,,

Whatever happens...I will not give up...btw, Ninjatwinkle13...that's the point...since you said that im not ready..it made the problem much more interesting to fix.hehe...

I'll definitely make this right! Im off senpais! Thank you for the replies!
Arigato!!!

"Everyday I learn something new from my superiors.."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.