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.

Recommended Answers

All 14 Replies

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

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

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

#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;
  }
}

Please review your logic it is seriously flawed

Tell me where?

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;
}

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;
}

Okay, but still stuck!

I fixed everyones code tags.

commented: Then use the comment in EDIT so we know what you did IN the post! +17

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

What do I do?

what output do you get now

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.

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.