-
I have a question
I just signed up for this site.. so i hope this works
Im in the middle of writing a program and i kinda hit a small brick wall..
I need to be able to replace all the letters in a string/array to '*'. Itried many ways and I have had no luck.. if you guys know how could u help a brother out
thanks
-
Post what you have tried in [code][/code] tags, explain what is not working, including any compiler errors or input and incorrect output.
The best way to accomplish your task depends a lot on what you are using and what you are familiar with.
-
k
well ill explain my problem.
Im doing a hangman game where the user inputs a name and the amount of tries it takes to reach the answer. now so far I have this.
int main ()
{
std::cout<<"enter:"<<endl;
std::cin>>string1;
int len = string1.length();
cout<<len<<endl;
int j =0;
for (j=0; j <len; j++)
{
cout<<"*";
}
cout<<endl;
cout<<"enter a char to be guessed: "<<endl;
cin>> n;
cout<<endl;
as you can see from the code I need to put '*' in replace of the letters. I did that.. Now all I need to know is how to replace each * with the quessed letter. I hope this makes sence. man im no good at this.
lol
-
Code:
int main( void )
{
std::string Word = "WWWWWWWWWWWWWWWWW";
std::cout<< '\n' << "Enter a word: ";
// std::cin >> Word; // I would use a getline for this
// getline( cin, Word );
Word.replace( Word.begin(), Word.end(), Word.size(), '*' );
std::cout << '\n' << Word
<< '\n';
return 0;
}
is an option too ...
Edit -
Please use code tags -
[code]Put these around whatever code you're posting so make it nice and neat![/code]
-
You can't really replace the *'s in the output with letters very easily in standard code. You can look into non-standard console functions (search for adrianxw's console tutorial) or you can just re-output the whole thing each time.
I might have two strings, one with *'s and one with the actual word. When the user gets a letter right, change the * to that letter in the first string. Then output the string each time.
-
You'll need a second string to be the guessing string. As the game progresses, you need to change it to the word string.
For evey guessed letter, you need to search the word for that letter. If it is, you'll need the index of that letter in the string to tell you were to put it in you guessing string. For some letters there may be more than one index, so you'll need to work that in too.
-
ooo ic
thanks
man this thing rules.. imma use it more oten now hehe