0% found this document useful (0 votes)
68 views2 pages

Source Code

This C++ source code implements a hangman game where the user tries to guess a randomly selected fruit name within a certain number of tries. It initializes variables like the secret word, the user's guessed letters, and the number of incorrect guesses. In a loop, it prompts the user for a letter, checks if it's in the secret word, updates the guess display and counters, and ends the game if the word is guessed or the tries are exceeded. A helper function checks the letter against the words and returns the number of matches.

Uploaded by

Srinjoy Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views2 pages

Source Code

This C++ source code implements a hangman game where the user tries to guess a randomly selected fruit name within a certain number of tries. It initializes variables like the secret word, the user's guessed letters, and the number of incorrect guesses. In a loop, it prompts the user for a letter, checks if it's in the secret word, updates the guess display and counters, and ends the game if the word is guessed or the tries are exceeded. A helper function checks the letter against the words and returns the number of matches.

Uploaded by

Srinjoy Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SOURCE CODE

#include <iostream.h>
#include <cstdlib.h>
#include <ctime.h>
#include <string.h>
using namespace std;
const int MAX_TRIES=5;
int letterFill (char, string, string&);
int main ()
{
string name;
char letter;
int num_of_wrong_guesses=0;
string word;
string words[] =
{
"mango",
"pineapple",
"banana",
"watermelon",
"strawberry",
"apple",
"kiwi",
"raspberry",
"orange",
"pear"
};
srand(time(NULL));
int n=rand()% 10;
word=words[n];
string unknown(word.length(),'*');
cout << "\n\n Welcome to hangman...Guess a Fruit Name";
cout << "\n\n Each letter is represented by a star. ";
cout << "\n\n You have to type only one letter in one try";
cout << "\n\n You have " << MAX_TRIES << " tries to try and guess the word. ";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
cout << "\n\n Guess a letter: ";
cin>> letter;

if (letterFill(letter, word, unknown)==0)


{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;
}
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
if (word==unknown)
{
cout << word << endl;
cout << "Yeah!You got it! ";
break;
}
}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << "\n Sorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}
int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

You might also like