only revealing some letters at a time

Hi,
I need help with unhiding a letter from a hidden sentence if the user guesses a letter that is in the sentence.

For example if I put 'Hello World' as my sentence then the hidden sentence would be '----- -----' and if the user enters a letter such as 'e' then it would print out '-e--- -----'.

I put the code I have so far down below.

I have part of the code, which I think is somewhat in the right direction to what I want, however when I enter any letters at all, even if its not in the sentence, the whole sentence gets revealed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Example program
#include <iostream>
#include <string>
using namespace std;

int get_search_replace(string dashed, string &sentence) 
{
 //This is the part that is suppose to reveal the correct letters
 char guess;
 dashed.size();
 for(int i = 0; i < dashed.size(); i++){
     cout << "Guess a letter" << endl;
     cin >> guess;
	if (sentence[i] == guess)
	   dashed[i] = guess;
	   cout << dashed << endl;
}
}
void set_replace_string(string sentence, string *dashed) {
   sentence.size();
   for (int a = 0; a < sentence.size(); a++)
      if(sentence[a] <= 'z' && sentence[a] >= 'A')
	 sentence [a] = '-';
   *dashed = sentence;
}

void get_string(string *sentence) {
   string str;
   cout << "Enter a string: " << endl;
   getline(cin, str); 
   *sentence = str;  
}


int main () {
   string sentence,dashed;

   get_string(&sentence);

   dashed = sentence;
   cout << "Message is: " << sentence << endl;

   set_replace_string(dashed, &dashed);
   cout << "dashed is: " << dashed << endl;
   
   get_search_replace(sentence, dashed);

      } 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <limits>
using namespace std;

constexpr auto GUESSES = 3;

int main()
{
    std::cout << "Enter string: \n";
    std::string input{};
    getline(std::cin, input);
    std::vector<int> positions{};
    bool match = false;
    int guess{};

    while (guess < GUESSES)
    {
        std::cout << "Enter char to search \n";
        char c{};
        std::cin >> c;
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        for (auto citr = input.cbegin(); citr != input.cend(); ++citr)
        {
            if(*(citr) == c)
            {
                positions.push_back(std::distance(input.cbegin(), citr));
                //records the position of the match
                match = true;
            }
        }
        ++guess;
    }
    if (match == false)
    {
        std::cout << "No correct guess \n";
    }
    else
    {
        std::sort(positions.begin(),positions.end());
        for (size_t i = 0; i < input.size(); ++i)
        {
            if(!(std::find(positions.begin(), positions.end(), i) != positions.end()))
            //checks if i is a matching position
            {
                input[i] = '_';
            }
        }
        std::cout << input << '\n';
    }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

std::string& unhide( const std::string& phrase, std::string& masked_phrase, char c )
{
    for( std::size_t i = 0 ; i < std::min( phrase.size(), masked_phrase.size() ) ; ++i )
        if( std::tolower(phrase[i]) == std::tolower(c) ) masked_phrase[i] = phrase[i] ;

    return masked_phrase ;
}

char get_guess()
{
    char c ;
    if( std::cin >> c && std::isalpha(c) ) return c ;

    std::cout << "please enter a letter\n" ;
    return get_guess() ;
}

int main()
{
    const std::string phrase = "Hello World!" ;
    std::string masked_phrase ;
    for( char c : phrase ) masked_phrase += std::isalpha(c) ? '-' : c ;

    while( masked_phrase != phrase )
    {
        std::cout << "phrase: " << masked_phrase << "\nguess a letter: " ;
        unhide( phrase, masked_phrase, get_guess() ) ;
    }

    std::cout << masked_phrase << '\n' ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;


int main()
{
    char guess;
    string phrase = "Hello World!" ;
    string masked = phrase;
    replace_if( masked.begin(), masked.end(), []( char  c ) { return isalpha( c ); }, '-' );

    while( masked != phrase )
    {
        cout << masked << "   Guess a letter: " ;
        cin >> guess;   guess = tolower( guess );
        transform( phrase.begin(), phrase.end(), masked.begin(), masked.begin(), [guess](char  c, char d) { return tolower(c) == guess ? c : d; } );
    }

    cout << masked;
}
Topic archived. No new replies allowed.