I need some help with my code.
How can I allow the player to guess the word if they'd want to, instead of going through and guessing letters when they already know the word.
Code:
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
void type_of_word(char f);
int main()
{
char c, ch, ch1, ch2;
char word[50];
char word1[25];
int l, i, next_guess, n, m, x;
do
{
do
{
cout<<"\n\n\n\t********************************************************\n";
cout<<"\t********************* WoRd GuEsSeR *********************\n";
cout<<"\t********************************************************\n\n\n";
cout<<"\n \t(E) Enter your own word\n\n \t(C) Computer Chosen Word @ Random\n\n \t(Q) Quit\n\n\n Enter your choice: ";
cin>> ch2;
}
while (ch2 != 'C' && ch2 != 'c' && ch2 != 'E' && ch2 != 'e' && ch2 != 'Q' && ch2 != 'q'&& ch2 != 'A' && ch2 != 'a');
if (ch2 == 'Q' || ch2=='q') exit (0);
if (ch2 == 'C' || ch2=='c')
{
ifstream fin("words.txt");
if (!fin)
{
cout<<"\n\nYou are missing a file called words.txt\n\n";
return 0;
}
for (i=0;!fin.eof();i++) fin.getline(word,25);
fin.close();
do
{
x=rand();
}
while(x>i || x<0);
ifstream finn("words.txt");
for (i=0;!finn.eof();i++)
{finn>>c; finn.getline(word,25); if (x==i) break;}
finn.close();
}
if (ch2 == 'E' || ch2=='e')
{
cin.get();
cout<<"\tType the word : ";
cin.getline (word, 25);
}
if (ch2 == 'E' || ch2=='e' || ch2 == 'C' || ch2=='c')
{
l=strlen(word);
char choosen[50]="\0";
n=0;
m=0;
for (i=0; i<=24; i++)
{
if (word[i]=='\0') {word1[i]='\0'; break;}
if (word[i]==' ') {word1[i]=' '; n++;}
if (word[i]!=' ') word1[i]='-';
}
next_guess=l+2-n; //Guess left, 2 more
do
{
there: type_of_word(c);
if (m!=0) cout<<"\n\n\t\t\tChoosen letters : "<<choosen<<"\n";
cout<<"\n\n\n\t\t\t "<<word1<<"\n\n\nYou have "<<next_guess<< " guesses left, choose a letter : ";
cin>>ch; cin.get();
for (i=0;i<25;i++)
if (choosen[i]==ch)
{
cout<<"\a\t\t !!You have choosen "<<ch<<" already!!\n"; goto there;
}
next_guess--;
choosen [m]=ch;
choosen [m+1]=',';
m+=2;
for (i=0;i<=49;i++)
if (word[i]==ch || word[i]==ch+32 || word[i]==ch-32) word1[i]=ch;
if (!strcmpi (word1,word)) {cout<<"\n\t\t\t "<<strupr(word)<<"\n\n\t\t\tCongratulations, You have won!\n"; break;}
}
while(next_guess>0 || !strcmpi (word1,word));
if (strcmpi (word1,word)) cout<<"\nSorry, maybe next time.\n\nThe word was : "<<strupr(word)<<endl;
}
cout<<"\nWould you like to play again? [Y or N] : ";
cin>>ch1; cin.get();
}
while (ch1=='y' || ch1=='Y');
system("PAUSE");
return 0;
}
void type_of_word(char f)
{ if (f=='p') cout<<"\t\t\t\tPROGRAMMING";
if (f=='s') cout<<"\t\t\t\tSPORT";
if (f=='t') cout<<"\t\t\t\tTV SHOW";
}