Loops

I am having trouble seeing why my loop is not terminating. Anybody have any hints? The loop I am talking about starts on line 28.

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
string game_play()
{
    char word[25];//Creates memory for the word that is to be guessed
    char blank[25];//creates blank spaces
    char guess;
    cout<<player_one()<<" please enter a word or phrase with 25 or less characters"<<endl;
    int counter=0; //simple counter
    int correct=0;//1=correct answer, 0=wrong answer
    cin.getline (word,25);
    strlen(word);
    cout<<"The word is "<<strlen(word)<<" characters long!"<<endl;
    int length=strlen(word); //stores the length of the word or phrase
    for (counter = 0; counter < length; counter++)//converts the letters to uppercase
        {		
           word[counter] = toupper(word[counter]);
       	}
    strcpy(blank,word);//Copies the word to the 'blank' array
    for (counter = 0; counter < length; counter++) 
    {			
           if (isalnum(word[counter])) blank[counter] = '_';	//converts characters to _'s to represent blanks 	
   	       else blank[counter] = word[counter]; 
    }
   	char player_two[20];
    cout<<"Player 2 please enter your name"<<endl;
    cin>>player_two;
    cin.get();
          //play game until the 'blank' puzzle becomes the 'right' answer 	
       do	{
       	   cout<<"The current blank puzzle is: "<<blank<<"."<<endl;	
       	   cout<<player_two<<" enter a guess."<<endl;		
           cin>>guess;		
           guess = toupper(guess);  				
           for (counter = 0; counter <= length; counter++) //check guess
           { 				
              if (guess == word[counter]) 
               { 				
              	blank[counter] = guess;		//fill in the puzzle with the letter 			
       	       } 		
           }			
                      
        }
        while (word != blank);    	                   	
    cout<<"Winner!";
   	cin.get();
}  
while (word != blank);

word never equals blank as word and blank are the pointers that point to different locations.

Use strcmp function to compare c-strings.
Last edited on

 
while (word != blank);   

This a VERY common mistake. What you have to remember is that the name of a string is actually the address of that string.
What you are doing is saying
"while the address of word is the same as the address of blank, do this"
Those addresses will never be the same, since the actual location of the strings in memory will always be different.
Try using the strcmp() function to compare the two strings, the return value will be 0 if the two strings are the same.
edit...
oops, answered too late.
Last edited on
Topic archived. No new replies allowed.