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.