Crazy looping, not going in

Why is it not going into the second loop??


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
  char word_validation (char word_player[],int word_size)
{
    char valid_word;
    int i;
    // 0 = false	   
    for (i=0; i < word_size; i++)
    {
        if (word_player[i] >= 'a' && word_player[i] <= 'z')
		{	valid_word = 'y';   
			printf("test1" );	
		}
    }   
    
	for (i=0; i < word_size; i++)
    {
		if(word_player[i] <= 'a' && word_player[i] >= 'z')
		{
			valid_word = 'n';
			printf("test2" );
		}	
	}
		
    printf("%c.\n", valid_word ); 
    
    if (valid_word != 'y')
    {
        printf("Invalid Character.\n");
        printf("Enter only letters.\n");
    }
    
    if (word_size < 3)
    { 
        printf("Word has less than 3 letters.\n"); 
        printf("Enter a valid length word.\n"); 
        valid_word = 'n';
    }       
    
    return(valid_word);
}
Last edited on
line 14
ive been doing the assigmnent for 50hrs +, its over 300 lines,
if was a real job I was fired lol
but I only been doing for 3 months
line 16
word_player[i] <= 'a' && word_player[i] >= 'z'
should be
word_player[i] < 'a' || word_player[i] > 'z'

I don't know what includes you have, but you could probably use the islower() function instead of those comparisons.

Also, is there any reason valid_word is a char and not a bool?
Last edited on
Second loop.

1
2
3
4
5
6
7
for (i=0; i < word_size; i++)
    {
		if(word_player[i] <= 'a' && word_player[i] >= 'z')
		{
			valid_word = 'n';
			printf("test2" );
		}	


Not sure what you are trying to do in this program, but it this meant to exclude letters a-z because that's what it is doing. It will never execute the if block if the word_player[i] is between 'a' and 'z'
Last edited on
OMG...that is right...I knew it was silly..im blind, prob stress...thanks all
If the user all ready used the letter.
how do i print a warning msg!! u alreay used it!!
thanks for looking

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
  void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{    
	const int SIZE2 = 10;
    char guess[SIZE2]; 
    char LetterEntered,valid;
    int x;
       
    do{
        
		printf("Enter a guess letter: ");

        fgets(guess, word_size, stdin);
        LetterEntered = guess[0];
		LetterEntered = tolower(LetterEntered);
        //printf("got letter %c\n",LetterEntered);
        
        
        if(LetterEntered >= 'a' && LetterEntered <= 'z')
        {   
            valid = 'y';
            for (x=0; x < word_size; x++) 
			{
                if(word_player[x] == LetterEntered) 
                    underscore[x] = LetterEntered;
            } 
			printf("Now the word is %s\n",underscore);
            
			for (x=0; x < word_size; x++) 
			{
                if(word_player[x] != LetterEntered) 
                    letter_used[guessed] = LetterEntered;
            } 
			printf("Letters used %s\n",letter_used);
								
        }
        else
            printf("Enter a char.\n");
            
    }while(valid != 'y');
    return;
}
I suppose you would use your letter_used array and run a check on it. If it is in there then output warning message.
I did it and didnt work!! its frustrating!!

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 guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{    
	const int SIZE2 = 10;
    char guess[SIZE2]; 
    char used[SIZE2] = {'\0'};
    char LetterEntered,valid;
    int x;
       
    do{
        
		printf("Enter a guess letter: ");
        fgets(guess, word_size, stdin);
        LetterEntered = guess[0];
		LetterEntered = tolower(LetterEntered);
        //printf("got letter %c\n",LetterEntered);
        
        
        if(LetterEntered >= 'a' && LetterEntered <= 'z')
        {   
            valid = 'y';
			
			for (x=0; x < word_size; x++) 
			{
                if(letter_used[x] == LetterEntered) 
                    letter_used[guessed] = LetterEntered;
            } 
			printf("you all ready used  %c\n",LetterEntered);	
			
			for (x=0; x < word_size; x++) 
			{
                if(word_player[x] == LetterEntered) 
                    underscore[x] = LetterEntered;
            } 
			printf("Now the word is %s\n",underscore);
            
			for (x=0; x < word_size; x++) 
			{
                if(word_player[x] != LetterEntered) 
                    letter_used[guessed] = LetterEntered;
                    used[guessed] = LetterEntered;
            } 
			printf("Letters used %s\n",letter_used);
								
        }
        else
            printf("Enter a char.\n");
            
    }while(valid != 'y');
    return;
}

Is this supposed to be a hangman game?

I would rewrite the first function to be shorter like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char word_validation (char word_player[],int word_size)
{
    if (word_size < 3)
    {
        printf("Word has less than 3 letters.\n");
        printf("Enter a valid length word.\n");
        return('n');
    }
    char valid_word = 'y';
    for (int i = 0; i < word_size && valid_word == 'y'; ++i)
    {
        if (word_player[i] < 'a' || word_player[i] > 'z')
            valid_word = 'n';
    }
    if (valid_word != 'y')
    {
        printf("Invalid Character.\n");
        printf("Enter only letters.\n");
    }
    return(valid_word);
}


I'm not exactly sure how letter_used and guessed are supposed to work. Assuming that letter_used has a maximum length of 26 (there are only 26 letters in the english alphabet) and guessed represents how many guesses have been made, I would rewrite your second function like this:
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
void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{
    if (guessed >= 26)
    {
        printf("Something's wrong, you can't have more than 26 guesses!");
        return;
    }
    const int SIZE2 = 10;
    char guess[SIZE2];
    char valid = 'y';
    do
    {
        printf("Enter a guess letter: ");
        fgets(guess, word_size, stdin);
        guess[0] = tolower(guess[0]);
        if (guess[0] >= 'a' && guess[0] <= 'z')
        {
            //check against letter_used
            for (int i = 0; i < guessed; ++i)
            {
                if (letter_used[i] == guess[0])
                {
                    printf("You already used %c.\n", guess[0]);
                    valid = 'n';
                    break;
                }
            }
            if (valid == 'y')
            {
                for (int i = 0; i < word_size; ++i)
                {
                    if (word_player[i] == guess[0])
                        underscore[i] = guess[0];
                }
                letter_used[guessed] = guess[0];
            }
        }
        else
            printf("Enter a char.\n");
    } while (valid != 'y');
    return;
}
return should b only at the bottom of the function!!

if(LetterEntered >= 'a' && LetterEntered <= 'z')
is validating the char

thanks for looking
the problem now is only.
print a msg: you all ready used this letter!!
new one, almost there!!!
btw..im trying to make this line to work.
printf("you all ready used %c\n",LetterEntered);
LINE 23

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
53
54
55
56
void guess_letter (char word_player[], int word_size, char underscore[],char letter_used[], int guessed)
{    
	const int SIZE2 = 10;
    char guess[SIZE2]; 
    char used[SIZE2] = {'\0'};
    char LetterEntered,valid;
    int x,i=0;
       
    do{
        do{
			printf("Enter a guess letter: ");
			fgets(guess, word_size, stdin);
			LetterEntered = guess[0];
			LetterEntered = tolower(LetterEntered);
			//printf("got letter %c\n",LetterEntered);
			
			for (x=0; x < word_size; x++) 
			{
				if(letter_used[x] == LetterEntered) 
					letter_used[guessed] = LetterEntered;
			} 
			if(letter_used[guessed] = LetterEntered)	
				printf("you all ready used  %c\n",LetterEntered);
			else	
			if(letter_used[x] != LetterEntered) 
				i=1;	
		}while(i != 0);		
			
			if(LetterEntered >= 'a' && LetterEntered <= 'z')
			{   
				valid = 'y';
				
				
				for (x=0; x < word_size; x++) 
				{
					if(word_player[x] == LetterEntered) 
						underscore[x] = LetterEntered;
				} 
				printf("Now the word is %s\n",underscore);
				
				for (x=0; x < word_size; x++) 
				{
					if(word_player[x] != LetterEntered) 
						letter_used[guessed] = LetterEntered;
						used[guessed] = LetterEntered;
				} 
				printf("Letters used %s\n",letter_used);
									
			}
			else
				printf("Enter a char.\n");
				
    }while(valid != 'y');
    
	return;
}
I know it should b
if(letter_used[guessed] == LetterEntered)
but then infinite looping
Add i=0 after line 10.
Also get rid of line 25.
Topic archived. No new replies allowed.