Compare strings to find similar characters

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
//Check Guess
        for ( int k = 0 ; k < 4 ; k++ )
        {
            int prevCharTorF = 0;
            //Correct Position AND Color     
            if ( guess[k] == answer[k] ) 
            {
                          cout << "B";  
                          cPosCol++ ; 
               
                          //****WIN GAME
                          if ( cPosCol == 4 ) 
                          {
                               cout << "Congratulations!!!!  You Guessed my color pattern!"
                                    << endl
                                    << "Would you like to play again? [Y/N] :";
                                //Pause. RETURN_EXIT.
                                system("PAUSE");
                                return 0;
                          }                           
            }
//HELP!!!!!!!!!!!!!!!!!!!!!!!!!!!!!          
            //Correct Color ONLY   
            for ( int l = 0 ; l < 4 ; l++ )
            {       
                    if ( k > 0 && prevCharTorF == 0 )//if we are not on 1st character - check to see if curChar has been used (only check once)
                    {
                         //Check for most recent same character
                         for ( int m = k - 1 ; m > -1 ; m-- )
                         {
                             if ( guess[k] == guess[m] ) //if curChar is the same as a previous char
                             {
                                  l = position[m] + 1; //Start looking at one after previous hit
                                  prevCharTorF++;
                                  break;               //break with new position
                             }
                         }
                         
                    }
                    
                    //Check for similar character in answer 
                    if ( guess[k] == answer[l] )
                    {    
                         position[k] = l;                     
                         cCol++;
                         break;
                    }
            }
        }


Here is my problem... When you are trying to find the correct color only the code checks to see if a same character has been used. If the program finds a previous same character that did not return a position it will loop through the entire answer guess. this causes problems when you have 3 similar characters. say the first one finds a hit; the second one begins looking after that hit and does not find anything; now the third one will see that the second character's array position is null. now it will check the entire answer string returning false positives. any recommendations on how to get it to terminate if the loop returns null?
Topic archived. No new replies allowed.