comparing strings issue

what i would like to do is take the string cpyString and if the element matches with the user input userGuess then replace the single letter with the guess.


1
2
3
4
5
for (int a = 0 ; a <=  wordLength ; a++)
{
     if (cpyString[a] = userGuess[0])
      newString[a] = userGuess[0];
}
Apart from having = instead of ==, what is wrong with that code?
it will change the entire string into my input i have no idea why
or it wont change anything so i have newString equal to a bunch of ******* i want to change these to the letter that matches cpyString. For example if cpyString = word then newString = **** if userGuess = w i want it to display w*** as the guesses go on it should be w*r* untill newString = word
Last edited on
As I said, you have = instead of ==.
that didnt seem to fix teh problem either tho my updated function
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
void userInput (const char * word)
{
        using namespace std;
        char userGuess[2];
        int wordLength = strlen (word);
        char newString [ wordLength + 1 ] ;
        char cpyString [ wordLength + 1 ] ;
        bool alpha;

        strcpy (cpyString, word);
        fill_n(newString, wordLength, '*');
        newString[ wordLength + 1 ] = 0 ;

        for (int i = 7; i > 0 && i <= 7 ; i--)
        {
                cout << "Here is the word" << newString << endl;
                cout << "What letter do you guess for the " << wordLength << " letter word.\n"
                     << "You have " << i << " guesses left. \n";
                cin.ignore( 2, '\n' ) ;
                cin.get ( userGuess[0] ) ;
                while (isalpha( userGuess[0] != 0))
                {       
                        cout << "Your guess must be a letter. Please enter a letter: " ;
                        cin.get( userGuess[0] ); 
                }       
                userGuess[1] = 0;
                tolower( userGuess[0] );  //make char lower case letter to match 

                for (int a = 0 ; a <=  wordLength ; a++)
                {
                        if (cpyString[a] == userGuess[0])
                                newString[a] == userGuess[0];
                }

        }
}
Now you have == where you need =.

= is assignment.
== is comparison.
ahh thank you it worked i forgot that small bit
Topic archived. No new replies allowed.