Hangman game, almost there!
Jul 31, 2011 at 4:08pm UTC
For some reason I am having issues printing off the letters correctly when guessed. Anyone help please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#include<iostream>
#include<fstream>
#include<string>
#include<ctime>
#include<Windows.h>
using namespace std;
void drawgallows(int );
int main()
{
int i,j, wordcount=0,state=0,totalcount=0, position=0;
string a[1000],b[1000];
char guessword[2],inword[20];
srand((unsigned int )time(NULL));
ifstream infile;
infile.open("C:/Users/OWNER/Desktop/words.txt" );
for (i=0;i<1000;i++)// Taking in all the words from the file which has secret words
infile>>a[i];
//infile.close("C:/Users/OWNER/Desktop/words.txt");
i = (rand()%100);//Finding a random word from the words.txt file
while (totalcount<6 && state<6)
{
cout<<"Guess next letter in the following word:" ;
for (int k=0;k<a[i].size();k++)//For printing the blanks and showing the length of the word
cout<<"_ " ;
cout<<"\n\nYour guessing letter:" ;//Asks the user for the guessing word
cin>>inword;
for (int j=1;j<=6;j++)// Breaks the string into words
{
b[j] = a[i].substr(j-1, 1);
if (inword==b[j])
wordcount++;
}
if (wordcount==0)
{ state++;// state is used for diagram(draw gallow) function
cout<<"Sorry but that was a great try" ;}
else
{totalcount++;
cout<<"Great guess\n\n" ;}
if (position==0)
cout<<inword<<" _ _ _ _ _ _" <<endl;
else if (position==1)
cout<<"_ " <<inword<<" _ _" <<endl;
else if (position==2)
cout<<"_ _ " <<inword<<" _" <<endl;
else if (position==3)
cout<<"_ _ _ " <<inword<<endl;
else if (position==4)
cout<<"_ _ _ _ " <<inword<<endl;
else if (position==5)
cout<<"_ _ _ _ _" <<inword<<endl;
else if (position==6)
cout<<"_ _ _ _ _ _" <<inword<<endl;
wordcount=0;
Sleep(1500);
drawgallows(state);
}
cout<<a[i]<<endl;
if (totalcount==6)
cout<<"\nYou have successfully guessed the word\n\n" ;
return 0;
}
//Will draw the gallows
void drawgallows(int state)
{
if (state==6)
{
cout<<endl<<endl
<<" +----+ " <<endl
<<" | | " <<endl
<<" | O " <<endl
<<" | /|\\ " <<endl
<<" | / \\ " <<endl
<<" |Your Dead " <<endl
<<" ============" <<endl<<endl;
}
else if (state==5)
{
cout<<endl<<endl
<<" +----+ " <<endl
<<" | | " <<endl
<<" | O " <<endl
<<" | /|\\ " <<endl
<<" | \\ " <<endl
<<" | " <<endl
<<" ============" <<endl<<endl;
}
else if (state==4)
{
cout<<endl<<endl
<<" +----+ " <<endl
<<" | | " <<endl
<<" | O " <<endl
<<" | /|\\ " <<endl
<<" | " <<endl
<<" | " <<endl
<<" =============" <<endl<<endl;
}
else if (state==3)
{
cout<<endl<<endl
<<" +----+ " <<endl
<<" | | " <<endl
<<" | O " <<endl
<<" | /| " <<endl
<<" | " <<endl
<<" | " <<endl
<<" =============" <<endl<<endl;
}
else if (state==2)
{
cout<<endl<<endl
<<" +----+ " <<endl
<<" | | " <<endl
<<" | O " <<endl
<<" | | " <<endl
<<" | " <<endl
<<" | " <<endl
<<" =============" <<endl<<endl;
}
else if (state==1)
{
cout<<endl<<endl
<<" +----+ " <<endl
<<" | | " <<endl
<<" | " <<endl
<<" | " <<endl
<<" | " <<endl
<<" | " <<endl
<<" =============" <<endl<<endl;
}
return ;
}
Last edited on Jul 31, 2011 at 4:09pm UTC
Jul 31, 2011 at 4:38pm UTC
So what is the issue you are having with the printing?
Jul 31, 2011 at 4:54pm UTC
When the program runs it won't print out what they have already guessed, so it will just keep asking for guessed letters and not show what has been done already. it does not display letters correctly when they are guessed correctly.
Jul 31, 2011 at 6:06pm UTC
I suggest you...
1. modify your load loop so it counts the actual number of words.
2. modify the display code so the letters that have already been guessed are displayed.
3. Replace this code; as it stands it makes no sense!
1 2 3 4 5 6
for (int j=1;j<=6;j++)// Breaks the string into words
{
b[j] = a[i].substr(j-1, 1);
if (inword==b[j])
wordcount++;
}
Last edited on Jul 31, 2011 at 6:06pm UTC
Topic archived. No new replies allowed.