Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ I need help solving this part of the problem. string reveal(string word, string letters) This function receives as input two strings: the word to

C++

I need help solving this part of the problem.

string reveal(string word, string letters)

This function receives as input two strings: the word to be guessed, and a string with all the correctly guessed characters. The job of this function is to create and return a new string with unguessed letters displayed as - and guessed letters are revealed. You are going to build a new string to be returned character by character by gluing or concatenating one letter at a time. Remember you can glue two strings together using the + (concatenation) operator.

Loop through each letter in the word to be guessed:

* if it appears in the guessed letter string then glue the letter onto the result.

*If not, glue -. You will need to use the string.find function to determine if a letter is in a string.

Remember it returns the position found, or string::npos if not found

Here is what i have.

#include "stdafx.h"

#include

#include

#include

using namespace std;

string pickWord();

string reveal(string, string);

int main()

{

string word, letters, ranWord;

word = pickWord();

reveal(word, letters);

return 0;

}

string pickWord()

{

srand(time(0));

int const nums = 10;

string word;

string fruits[nums] = { "apple", "bannana", "orange", "kiwi", "melon", "berries", "coconut", "papaya", "pineapple", "pair" };//random words

for (int i = 0; i < nums; i++)

{

//random word generator

fruits[i] = fruits[0 + rand() % 9];

word = fruits[i];

}

return word;//returns a random word

}

string reveal(string ranWord, string letters)

{

char guess;

string dash;

cout << "I'm thinking of a word with " << ranWord.length() << " letters" << endl;//find length of the word

for (int i = 0; i < ranWord.length(); i++)//display -----

cout << '-';

letters = "";

cout << endl;

cout << "Guess a letter: ";//guess

cin >> guess;

return ranWord;

}

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

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions