954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple hangman C++-stuck!

Hello, I joined about 5 minutes back. I need to complete my computer project, which is a hangman game in c++, by today. Based on what my teacher taught me, I have created a code that runs like this-

#include<iostream.h>//These are the only header files taught.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int  display(char [], int&);
int input(char [], char&, int&, int);
int l;

void main()
{ clrscr();
  int n,res,pos=0,state=0; char num[50],ch, M [][50]={ "Roman Holiday","Breakfast At Tiffany's", "Sweet November", "Only You","Love, Actually"};
  randomize();
  n=random(5);
  puts(M[n]);
  char s1[50];
  strcpy(s1,M[n]);
  display(s1,pos);
  cout<<"\n \nYou have "<<pos<<" blanks to fill.Good luck."<<endl;
  input(s1,ch,state,pos);
  cout<<state;
  getch();
  }
  int display(char s1[], int &pos)
   {
	for(l=0; s1[l]!='\0';l++)
	{
		if(s1[l]=='a'|| s1[l]=='e'|| s1[l]=='i'|| s1[l]=='o'|| s1[l]=='u'|| s1[l]=='A'|| s1[l]=='E'|| s1[l]=='I'|| s1[l]=='O'|| s1[l]=='U'|| s1[l]=='?'|| s1[l]=='!'|| s1[l]==','|| s1[l]=='\'')
			cout<<s1[l];
			else
			if (s1[l]==' ')
				cout<<'/';
				else
				 {
				  cout<<'_';
				  pos++;}     }
				  return pos;
				 }

int input(char s1[], char &ch, int &state, int pos)
{for( int j=0; j<pos; j++)
   {cin>>ch;
    l=0;
    int m=strlen(s1);
    for(l=0;l<=m;l++)
     {if(ch==s1[l])
	cout<<"'"<<ch<<"' is at the " <<l+1<<"th position."<<endl ;
	else
	state++;
	}
	       }
	       return state;
		}


state is a count of the mistakes the user makes, and is to have a max value of 6.I returned 'state' just to check if it works accordingly.However, my output is coming outrageously wrong, displaying numbers like 217, 33, 32 and so on.WHAT AM I DOING WRONG?

the program is meant to go like-

void input(char s1[], char &ch, int &state, int pos)
{for( int j=0; j<pos; j++)
   {cin>>ch;
    l=0;
    int m=strlen(s1);
    for(l=0;l<=m;l++)
     {if(ch==s1[l])
	cout<<"'"<<ch<<"' is at the " <<l+1<<"th position."<<endl ;
	else
	{state++;
	hangman(state);
	}
	       }

		}


		}


void hangman(int state);
{if (state<=6)
  {
   char s2[]="HANGMAN";
   for(l=0;l<=state;l++)
	cout<<s2[l];
	  }

	  }


There are no syntactical errors. But the program MAKES NO SENSE!
HELP!
Also,
i)to make this case INsensitive, what do i do?
ii)how do i make sure that state doesn't exceed 6? the output should be "YOU FAIL",and the program should end.

stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 
int input(char s1[], char &ch, int &state, int pos)

Here &state means address of state, and you are passing value of state here :input(s1,ch,state,pos);

There is no need to pass address of state since you are returning state.
If you want to pass by reference then you will have to pass address when you call a function ..ex . someFunction(&a) and in the definition you will catch it in a pointer like say,

void someFunction(int *p)
{
   //and you will access its value here using *p which means value at address contained in poiter p
}

See which one suits your needs, n modify your code accordingly.

Also next time when you post a code use code tags

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

I did that, now state comes zero every time, irrespective of mistakes.

stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

What did you do ..which change? post your code again USE CODE TAGS AND FORMAT IT so that it will be easier to understand. And post what output you are getting so far and what output you are expecting

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void hangman(int );
int  display(char [], int&);
int input(char [], char&, <blockquote>int</blockquote>, int);
int l;

void main()
{ clrscr();
  int n,pos=0,<blockquote>s=0</blockquote>; char num[50],ch, M [][50]={ "oman holiday","breakfast at tiffany's", "sweet november", "only you","love, actually"};
  randomize();
  n=random(5);
  puts(M[n]);
  char s1[50],s2[50];
  strcpy(s1,M[n]);
  display(s1,pos);
  cout<<"\n \nYou have "<<pos<<" blanks to fill.Good luck."<<endl;<blockquote>input(s1,ch,s,pos);</blockquote><blockquote>cout<<input(s1,ch,s,pos);</blockquote>
  getch();
  }
  int display(char s1[], int &pos)
   {
	for(l=0; s1[l]!='\0';l++)
	{
		if(s1[l]=='a'|| s1[l]=='e'|| s1[l]=='i'|| s1[l]=='o'|| s1[l]=='u'|| s1[l]=='?'|| s1[l]=='!'|| s1[l]==','|| s1[l]=='\'')
			cout<<s1[l];
			else
			if (s1[l]==' ')
				cout<<'/';
				else
				 {
				  cout<<'_';
				  pos++;}     }
				  return pos;
				 }

int input(char s1[], char &ch, int<blockquote>state</blockquote>, int pos)
{for( int j=0; j<pos; j++)
   {cin>>ch;
    l=0;
    int m=strlen(s1);
    for(l=0;l<=m;l++)
     {if(ch==s1[l])
	cout<<"'"<<ch<<"' is at the " <<l+1<<" position."<<endl ;
	else
	{state++;

	}
	       }
	 return state;
		}


		}
stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Please review your logic it is seriously flawed

Ali_2101
Newbie Poster
16 posts since Feb 2012
Reputation Points: 4
Solved Threads: 3
 

Tell me where?

stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

Try this

int input(char s1[], char &ch, int &state, int pos)
{
state=0;
//for( int j=0; j<pos; j++)
while(state!=6)
{
        int found=0;
        cin>>ch;
        l=0;
        int m=strlen(s1);
        for(l=0;l<=m;l++)
        {
                if(ch==s1[l])
                {
                        cout<<"'"<<ch<<"' is at the " <<l+1<<"th position."<<endl ;
                        found=1;
                }
        }
        if(!found)
                state++;

}
return state;
}
Ali_2101
Newbie Poster
16 posts since Feb 2012
Reputation Points: 4
Solved Threads: 3
 

Please use an adequate posting using the [ code][ /code] tags.

Stannum post:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void hangman(int );
int display(char [], int&);
int input(char [], char&,

int
, int);
int l;

void main()
{ clrscr();
int n,pos=0,

s=0
; char num[50],ch, M [][50]={ "oman holiday","breakfast at tiffany's", "sweet november", "only you","love, actually"};
randomize();
n=random(5);
puts(M[n]);
char s1[50],s2[50];
strcpy(s1,M[n]);
display(s1,pos);
cout<<"\n \nYou have "<<pos<<" blanks to fill.Good luck."<<endl;

input(s1,ch,s,pos);

cout<<input(s1,ch,s,pos);
getch();
}
int display(char s1[], int &pos)
{
for(l=0; s1[l]!='\0';l++)
{
if(s1[l]=='a'|| s1[l]=='e'|| s1[l]=='i'|| s1[l]=='o'|| s1[l]=='u'|| s1[l]=='?'|| s1[l]=='!'|| s1[l]==','|| s1[l]=='\'')
cout<<s1[l];
else
if (s1[l]==' ')
cout<<'/';
else
{
cout<<'_';
pos++;} }
return pos;
}

int input(char s1[], char &ch, int

state
, int pos)
{for( int j=0; j<pos; j++)
{cin>>ch;
l=0;
int m=strlen(s1);
for(l=0;l<=m;l++)
{if(ch==s1[l])
cout<<"'"<<ch<<"' is at the " <<l+1<<" position."<<endl ;
else
{state++;

}
}
return state;
}


}


Ali_2101 post

int input(char s1[], char &ch, int &state, int pos)
{
state=0;
//for( int j=0; j<pos; j++)
while(state!=6)
{
int found=0;
cin>>ch;
l=0;
int m=strlen(s1);
for(l=0;l<=m;l++)
{
if(ch==s1[l])
{
cout<<"'"<<ch<<"' is at the " <<l+1<<"th position."<<endl ;
found=1;
}
}
if(!found)
state++;

}
return state;
}
lucaciandrew
Junior Poster in Training
73 posts since Jan 2012
Reputation Points: 10
Solved Threads: 5
 

Okay, but still stuck!

stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

I fixed everyones code tags.

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

Thank you very much!
But my program is still failing:(.

stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

What do I do?

stannum
Newbie Poster
7 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

what output do you get now

Ali_2101
Newbie Poster
16 posts since Feb 2012
Reputation Points: 4
Solved Threads: 3
 

First thing's first. As mentioned, use proper FORMATTING. See this .

Without consistent formatting, trying to follow and understand someone else's program is difficult. If you need help, help us, too.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: