// hangman.cpp // Hangman game illustrates string library functions, // characte
ID: 3669534 • Letter: #
Question
// hangman.cpp
// Hangman game illustrates string library functions,
// character arrays, arrays of pointers, etc.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
using namespace std;
// Prototype. we'll define this below.
int processGuess(char* word, const char* targetWord, char guess);
// Define an array of strings (since a string is just a char array)
// and since string are just char *'s, we really want an array of char *'s
const char* wordBank[] = {"computer", "president", "trojan", "program",
"coffee", "library", "football", "popcorn",
"science", "engineer"};
const int numWords = 10;
int main()
{
srand(time(0));
char guess;
bool wordGuessed = false;
int numTurns = 10;
// Pick a random word from the wordBank
const char* targetWord = wordBank[rand() % numWords];
// More initialization code as needed
char word[80]; // a blank array to use to build up the answer
// It should be initialized with *'s and then
// change them to the actual letters when the
// user guesses the letter
// An individual game continues until a word
// is guessed correctly or 10 turns have elapsed
// Print out end of game status
return 0;
}
// Given the current version of the word, target/selected word, and
// the character that the user just guessed, change the word to
// "turn on" any occurrences the guess character and return a count
// of how many occurrences of 'guess' were found
int processGuess(char* word, const char* targetWord, char guess)
{
}
Explanation / Answer
const char* targetWord = wordBank[0];//rand() % numWords];
char word[80];
int len = strlen(targetWord);
strncpy(word, targetWord, 80);
for(int i=0;i<len;i++)
{
word[i] = '*';
}
while(numTurns != 0 && wordGuessed == false)
{
cout << "Current word: " << word << endl;
cout << numTurns << " remain... Enter a letter to guess:" << endl;
cin >> guess;
processGuess(word, targetWord, guess);
int turnCount = processGuess(word, targetWord, guess);
if(turnCount == 0)
{
numTurns--;
}
if(strcmp(word,targetWord)==0)
{
wordGuessed = true;
}
}
if(wordGuessed == true)
{
cout << "The word was: " << targetWord << ". You win!" << endl;
}
else
{
cout << "Too many turns. You lose!" << endl;
}
return 0;
}
int processGuess(char* word, const char* targetWord, char guess)
{
int len = strlen(targetWord);
int count = 0;
for(int i = 0;i<len;i++)
{
char targetLetter = targetWord[i];
if(strcmp(&guess, &targetLetter)==0)
{
*(word+i) = guess;
count++;
}
}
if(count == 0)
{
return 0;
}
else
{
return 1;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.