Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- string word;
- void randomWord()
- {
- srand(time(NULL));
- int num=rand()%27+1;
- string words[28];
- string text;
- ifstream file("words.txt");
- for(int c=0;file>>text;c++)words[c]=text;
- word=words[num];
- }
- int main()
- {
- randomWord();
- int count=0;
- string text;
- string theWord[word.length()];
- string guessedLetters="";
- for(int c=0;c<word.length();c++)theWord[c]+="?";
- int hangman=0;
- while(hangman!=7)
- {
- if(count==0)cout<<"Your word is "<<word.length()<<" letters long!"<<endl;
- if(count==0)for(int c=0;c<word.length();c++)cout<<theWord[c]<<" ";
- else
- {
- for(int c=0;c<word.length();c++)cout<<theWord[c]<<" ";
- cout<<"\t\tGuessed letters: ";
- for(int c=0;c<count;c++)cout<<guessedLetters[c];
- }
- cout<<endl;
- if(count==0)cout<<"If you would like to guess a letter, type in one,\nif you would like to guess the word, type 'guess' ";
- else cout<<"Enter your guess: "<<endl;
- cin>>text;
- while(text.length()>1&&text!="guess")
- {
- cout<<"Invalid input! Please enter a chracter or 'guess': ";
- cin>>text;
- }
- while(guessedLetters.find(text)!=string::npos)
- {
- cout<<"You already guessed this letter! Enter another one: ";
- cin>>text;
- }
- guessedLetters+=text;
- if(text=="guess")
- {
- string guess;
- cout<<"What do you think the word is? ";
- cin>>guess;
- if(guess==word)
- {
- cout<<"Congratulations! You guessed the word correctly!"<<endl;
- return 0;
- }
- else
- {
- cout<<"Sorry! That's incorrect! The word was "<<word<<". Better luck next time."<<endl;
- return 0;
- }
- }
- if(word.find(text)!=string::npos)
- {
- int num=word.find(text);
- theWord[num]=text;
- int num2=word.rfind(text);
- theWord[num2]=text;
- }
- else
- {
- cout<<"Letter not found!"<<endl;
- hangman++;
- }
- count++;
- }
- cout<<"Sorry! You lost!"<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment