Output Problem

I want to check if a char is one of the letters in a word(string), so I wrote like this:

1
2
3
4
for (int i = 0; i < word.length(); i++) {
   if(letterForScan == word.charAt(i))
   cout << "correct" << endl;
}


However, if the word contains two or more same letters, the output would be corresponding times of "correct". For example, if the word is "character" and the letterForScan is a, there would have two "correct" output. How could I make it only show "correct" only once?
Last edited on
use break;

1
2
3
4
5
 if ( true )
{
//....
break;
}
if i use "break", how can i output two same letter at the same time? when i try, it would only output one letter at a time, but i want to show all same letters in a word once the guess is entered correctly.

Without using "break" it shows like this:
1
2
3
4
Correct!
w________
Correct!
w____w___


Also, for convenience, i write:
 
char [] lettersGuessed = {'_', '_', '_', '_', '_', '_', '_', '_', '_'};

but the words are randomly selected, not always will be 9 letters. Is there a way to make those underscore changes to the number corresponding to the word length?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
int b = 0;

for (int i = 0; i < word.length(); i++) {
   if(letterForScan == word.charAt(i))
       b++;  
}
if(b>0) cout << "correct" << endl;

for(int i=0; i<b; i++)
  cout << letterForScan;
Last edited on
Topic archived. No new replies allowed.