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();
}