Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Enter your name as a comment for program identification // Program assignment HangMan.cpp // Enter your class section, and time /* The program HangMan.cpp

// Enter your name as a comment for program identification // Program assignment HangMan.cpp // Enter your class section, and time

/* The program HangMan.cpp is a program that prompts the user for a word and then allows the user to enter a letter. The word is searched for the letter. The user can enter more letters until a zero is entered. The program displays the number of times a letter is found, and all the letters found in the word. */ /* The user inputs a word, letters to search the word, and a zero to quit. */ /* The number of times a letter appears and all the letters found in the word are displayed. */ /*********************************************** ************************************************ YOUR ASSIGNMENT IS CHANGE THIS GAME-HANGMAN TO A C PROGRAM. THAT MEANS YOU CAN NOT USE THE string AS DATA TYPE AND PAPAMETER PASSINGS SHOULD BE BY POINTER WHENEVER POSSIBLE. THE RESULTS SHOULD BE THE SAME. PROPER COMMENTS AND INDENTATION WILL BE CONSIDERED AS PART OF THE GRADE. ************************************************ ************************************************/ #include #include

using namespace std;

// function prototypes /* The function instruct describes the use and purpose of the program. */ void instruct();

/* the function getWord reads the word entered */ string getWord();

/* the function findLetter searches the word for a letter and returns the number of times the letter appears in the word */ int findLetter (char letter, int number, string word);

/* the function getGuess prompts the user for the letter and returns the letter */ char getGuess();

/* the function display displays the letters found in the word */ void display (const bool alphabet[]);

int main() { // declare variables /* a string variable word to read in the word, a character variable guess to enter the letter, an integer variable number for the length of the word, an integer variable occurrence for the number of times the letter appears in the word, and a Boolean array alphabet to indicate if the letter has been found*/ string word; char guess; int number, occurrence; bool alphabet[26] = {false}; // call the function instruct instruct(); // call the function getWord to read in the word word = getWord(); // find the length of the word number = word.length(); // display a message showing the length of the word cout << "The word is " << number << " characters "; // call the function getGuess to read the letter guess = getGuess();

// loop until a 0 is entered while (guess) { /* call the function findLetter to find the number occurrences of the letter in the word */ occurrence = findLetter(guess, number, word); // display the number of occurrences of the letter cout << "There are " << occurrence << ' ' << guess <<"'s. "; /* set the array element corresponding with the letter to true */ if (occurrence) alphabet[guess-97] = true; // call the function getGuess to read the letter guess = getGuess(); } display (alphabet);

return 0; }

// function prototypes /* The function instruct describes the use and purpose of the program. */

void instruct() { cout << "This program prompts the user for " << "a word and then a letter. " << "The word is searched to find the letter. " << "The user can enter more letters until a " << "zero is entered. The program displays the " << "number of times a letter is found, and all " << "the letters found in the word. "; }

/* the function display displays the letters found in the word */ void display (const bool alphabet[]) { cout << "You found these letters "; /* loop through the array for the letters found */ for (int count = 0; count < 26; count++) { if (alphabet[count]) cout << (char)(count + 97) << '\t'; } cout << endl; }

/* the function getWord reads the word entered */ string getWord() { string word; cout << "Please enter a word: "; cin >> word; return word; }

/* the function getGuess prompts the user for the letter and returns the letter */ char getGuess() { char guess = 0; while (guess < 97 || guess > 122) { cout << " What letter would you like to guess? (Enter zero to quit.) "; cin >> guess; guess = tolower(guess); if (guess == '0') return 0; } return guess; } /* the function findLetter searches the word for a letter and returns the number of times the letter appears in the word */ int findLetter (char letter, int number, string word) { // declare variables /* declare an integer variable foundLetter initialized to zero to count the number of times the letter appears in the word, an integer variable start initialized to 0 to indicate the starting search location in the word, an integer variable stop intialized to the length of the word to indicate the end location in the word, a string variable newWord initialized to word to hold the current word to be searched, a string::size_type variable position to indicate if a match has been found */ int foundLetter = 0, start= 0, stop = number; string newWord = word; string::size_type position; // loop through for all uppercase and lowercase for (int count = 0; count < 2; count++) { // loop until the word is at the end while (stop) { // assign the location in the word of the letter position = newWord.find(letter); if (position == string::npos) stop = 0; else { /* begin from the location where the last match occurred */ start = newWord.find(letter); // increment the letter counter foundLetter++; // adjust the ending of the search stop = stop - start; // search the rest of the word newWord = newWord.substr(start+1,stop); } } // reinitialize the size of the string stop = number; // change the letter to uppercase letter = toupper(letter); // reinitialize newWord to the word newWord = word; } // return the number of occurrences of the letter return foundLetter; }

/* Please enter a word: howdy The word is 5 characters

What letter would you like to guess? (Enter zero to quit.) a There are 0 a's. What letter would you like to guess? (Enter zero to quit.) e There are 0 e's. What letter would you like to guess? (Enter zero to quit.) d There are 1 d's. What letter would you like to guess? (Enter zero to quit.) o There are 1 o's. What letter would you like to guess? (Enter zero to quit.) y There are 1 y's. What letter would you like to guess? (Enter zero to quit.) b There are 0 b's. What letter would you like to guess? (Enter zero to quit.) 0 You found these letters d o y */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions