Solve the following Exercises. Upload your homework to blackboard in zip file Fo
ID: 3923844 • Letter: S
Question
Solve the following Exercises. Upload your homework to blackboard in zip file For each problem include: Pseudo code The C++ source code Problems: Create a program "guess the Word" The user will try to guess a word defined by the programmer (you define the word) The user will roll a dice (6 sided dice), depending of the result will be the number of attempts for the user. Each attempt the user can input a letter. If the letter belongs to the word the program will display the letter in the correct location. At the end of the total attempts the user can guess the word. If the user guess correctly the word the user winsExplanation / Answer
// C++ code guess the word
#include <cstdlib>
#include <iostream>
#include <stdlib.h> /* srand, rand */
#include <string.h>
#include <algorithm>
#include <fstream>
using namespace std;
int main()
{
string guess;
int wrong=1;
string inputWord;
string letternotfound;
srand (time(NULL));
int attempts = rand()%6 + 1;
cout << "Enter the word to be guessed: ";
getline(cin, inputWord);
string inputWordcopy = inputWord;
for(int i=0; i!=inputWord.length(); i++)
{
if(inputWord.at(i) == ' ')
{
letternotfound += " ";
}
else
{
letternotfound += "_";
}
}
cout << endl;
while(1)
{
if(wrong == attempts)
{
cout << " You Lose! Word: " << inputWord << endl;
break;
}
cout << letternotfound << endl;
cout << "You have " << attempts - wrong << " attempt left" << endl;
if(letternotfound == inputWord)
{
cout << "Well done, You win!" << endl;
cout << "Word :" << inputWord << endl << endl;
break;
}
cout << "Guess a letter" << endl;
cin >> guess;
if(inputWordcopy.find(guess) != -1)
{
while(inputWordcopy.find(guess) != -1)
{
letternotfound.replace(inputWordcopy.find(guess), 1, guess);
inputWordcopy.replace(inputWordcopy.find(guess), 1, "_");
}
}
else
{
cout << "Letter not in word " << endl;
}
wrong++;
cout << endl;
}
return 0;
}
/*
output:
Enter the word to be guessed: mine
____
You have 5 attempt left
Guess a letter
m
m___
You have 4 attempt left
Guess a letter
q
Letter not in word
m___
You have 3 attempt left
Guess a letter
n
m_n_
You have 2 attempt left
Guess a letter
e
m_ne
You have 1 attempt left
Guess a letter
p
Letter not in word
You Lose!
Word: mine
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.