I'm try to make a program that will ask a user to input a string then the string gets masked (by asterisks) and later, guess every letter (character) of the masked word until the string is fully unmasked (a la Hangman). But my problem is, after the first unmasking, the letter(s) that has been unmasked returns to being masked(character unlocked becomes an asterisk again). I've made this work at school, but using a character of arrays (C string). I'm new to C++ Strings so please bear with me.

#include<cctype>
#include<iostream>
#include<string>

using namespace std;


string mask(string str)
{
     for(int i = 0; i < str.length(); i++)
     {
     
     if(isprint(str[i]))
     {
     str[i] = '*';
     }
     
     else
     {
     continue;
     }
     
     }
     
     return str;
     
     
     }
     
string unmask(char input, string str, string mask)
{

     for(int i = 0; i < str.length(); i++)
     {
     
     if(str[i] == input)
     {
     mask[i] = input;
     }
     
     
     }
            
     return mask;
       
     }

main()
{
      
      
     string str = "\0";
     // (take note: this is an unfinished program) int chance = 4;
     cout<<"\nEnter string for guessing: ";
     getline(cin, str, '\n');
     
     
     string c = mask(str);
     
     string x = c;
     
     while(x != str)
     { 
     char input;
     cout<<x;
     cout<<"\n\nEnter character: ";
     cin>>input;

     x = unmask(input, str, c); 
     
     
     cout<<"\n\n";    
     }
      
      
      
      
      getchar();
      getchar();
      }

Recommended Answers

All 3 Replies

Sorry... What is isprint() function for? From my first glance, you didn't save any chars while you are masking the incoming string...

Sorry... What is isprint() function for? From my first glance, you didn't save any chars while you are masking the incoming string...

isprint will return 1 if a character is a printable one, otherwise 0. Thanks bro, but I have already solved the problem, I noticed that instead of unmask(input, str, x) I put unmask(input, str, c).

Moderators / Admin = Mark this as solved please.

Actually there's a button at the bottom so that you yourself can mark it as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.