comparing two different strings question?

i have a program and was wondering if i could compare a variable of type firstword[4] to just word.
Example if(firstword[4]=secondword); i am writing hangman game and if the first word when its guessed completely and is same as secondword which is word to be guessed i want it to print congrats, else if not equal then print you lose
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
50
51
52
void guessword(string word)
{
    char letter;
    int position;
    
    string blankword[4];
    blankword[0] = "_ ";
    blankword[1] = "_ ";
    blankword[2] = "_ ";
    blankword[3] = "_ ";
    
   
       
     for (int i=0;i<6;i++)
     {
     cout << "What letter would you like to guess?";
     cin >>letter;
     
     position = word.find(letter);
     if (position > word.length())
        cout<<letter<< " is not in the word "<<endl;
     else 
     {
        cout<< letter << " is in the word"<<endl;
        if(position==0)
        {
         blankword[0] = letter;
         cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
        }
        else if(position==1)
        {
         blankword[1] = letter;
         cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
        }
        else if(position==2)
        {
         blankword[2] = letter;
         cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
        }
        else if (position==3)
        {
         blankword[3] = letter;
         cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
        }
     
     }

     }
   
     
   
}


it loops through and the guesses are entered into the blankword variable if the letter is in the word.
Last edited on
Hi, yes you can compare an element in an array to a variable of the element type
So
1
2
3
4
5
string aSimpleString;
string anArrayOfStrings[4];
...
if (aSimpleString==anArrayOfStrings[2])
..

is fine.

Note that where you have a series of if's on the same variable as in your code, a switch statement is often a good alternative.
See http://www.cplusplus.com/doc/tutorial/control.html - switch is discussed at the bottom of the page.
Last edited on
Topic archived. No new replies allowed.