- #1
hermy
- 41
- 0
Hangman Program! Why Doesn't It Work??
Hi! I wrote this program to play a game of hangman. There were no errors when I compiled it (using turbo c++, on windows), but it's not working the way it is supposed to.
The function dashnhang()worked perfectly well when I placed it and ran it n a separate program, but one line in it doesn't get executed.
Also, there is an infinite loop somewhere due to which the program never stops accepting a value from the user.
Here's the code: (It looks so long only because of the spaces and comments :) )
For the dat file, you can copy the stuff below to notepad and save it as hangman.dat:
detroit
orlando
bangalore
birmingham
chennai
hyderabad
phoenix
madrid
paris
tokio
Hi! I wrote this program to play a game of hangman. There were no errors when I compiled it (using turbo c++, on windows), but it's not working the way it is supposed to.
The function dashnhang()worked perfectly well when I placed it and ran it n a separate program, but one line in it doesn't get executed.
Also, there is an infinite loop somewhere due to which the program never stops accepting a value from the user.
Here's the code: (It looks so long only because of the spaces and comments :) )
Code:
#include <fstream.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>
#include <process.h>
/*______________________________________________________________________________
--------------------------------------------------------------------------------
PUZNO(): Reads and returns a puzzle word from the file "hangman.dat".
The user gives the puzzle no.(i.e. pno)
________________________________________________________________________________
-----------------------------------------------------------------------------*/
char* puzno(int pno)
{
char line[40];
ifstream p;
p.open("hangman.dat");
for(int i=1; i<=pno; i++)
p.getline(line, 40); //In the dat file, each word has been
return line; //placed in a different line.
}
/*_____________________________________________________________________________
-------------------------------------------------------------------------------
DASHNHANG(): Draws the dashes and the basic structure from which you
want to hang your man.
_______________________________________________________________________________
----------------------------------------------------------------------------*/
void dashnhang(int size)
{ int a,b;
detectgraph(&a,&b);
initgraph(&a, &b, "..\\bgi");
// Loop to draw dashes
for (int i=1; i<=size; i++)
line(55 + 30*i + 10*(i-1), 400, 55 + 30*(i+1) + 10*(i-1), 400);
//(55 & 400 r the initial x&y coordinate, 30=length of each dash, 10=space betn 2 dashes)
// To draw hangman
line(400, 120, 400, 160);
line(400, 120, 480, 120);
line(480, 120, 480, 320);
line(420, 320, 520, 320);
}
/*_____________________________________________________________________________
-------------------------------------------------------------------------------
OOPS(): Whenever you enter a letter which is not a part of the word,
this function draws the head, then a body, limbs, so on...
_______________________________________________________________________________
-----------------------------------------------------------------------------*/
void oops(int t, int size, char word)
{
switch(t)
{
case 8: {arc(400, 190, 30, 150, 10);
gotoxy(15, 15);
cout<<"BAD LUCK! \n The word is: "<<word;} // Frown
case 7: {circle(393, 170, 1); circle(407, 170, 1);} //The eyes
case 6: line(400, 230, 430, 200); //Hand
case 5: line(400, 230, 370, 200); //Hand
case 4: line(400, 260, 430, 290); //Leg
case 3: line(400, 260, 370, 290); //Leg
case 2: line(400, 190, 400, 260); //Body
case 1: circle(400, 175, 15); //Head
case 0: dashnhang(size);
}
if(t==8) exit(0);
}
/*_____________________________________________________________________________
-------------------------------------------------------------------------------
DISP(): If the letter you have entered is correct, then this
function is called. The correct letters are displayed
on the appropriate dashes.
_______________________________________________________________________________
-----------------------------------------------------------------------------*/
void disp(char c[40], int size)
{
for(int i=0; i<size; i++)
if(c[i]!=0)
{gotoxy(13+5*i, 25);
cout<<c[i];
}
}/*____________________________________________________________________________
------------------------------------------------------------------------------
Let's declare some variables we'll need...
______________________________________________________________________________
-----------------------------------------------------------------------------*/
char copy[40], word[40], c; int won, size, pno, t=0;
//____________________________________________________________________________
//----------------------------------------------------------------------------// Main starts here:
void main()
{
for(int cat=0; cat<40; cat++) //We create a string full of zeroes
copy[cat]=0;
cout<<"Enter a no. between 1 and 10: \n";
cin>>pno;
strcpy(word,puzno(pno)); //Word = puzno(pno)
size=strlen(word);
dashnhang(size);
while(t<8) // t = no.of wrong tries
{cout<<"pls enter a letter : "
cin>>c; cout<<"\n" // User guesses a letter
for (int i=0; i<size; i++)
if (c==word[i]) // Check if c is present in word
{copy[i]=c;
disp(copy, size);
won=strcmp(word, copy);
if (won==0) //If all letters hav been guessed
{gotoxy(15, 15); //display the message
cout<<"YOU WON!";
exit(0);
}
else
{t++;
oops(t, size, word);
}
}
}
}
detroit
orlando
bangalore
birmingham
chennai
hyderabad
phoenix
madrid
paris
tokio
Last edited: